Add initial version of serialisation to Song and Format.

crossfading
Nekojimi 3 years ago
parent a4e4093f40
commit e8727d163c
  1. 18
      nb-configuration.xml
  2. 22
      src/main/java/moe/nekojimi/chords/Format.java
  3. 44
      src/main/java/moe/nekojimi/chords/Song.java

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.license>gpl30</netbeans.hint.license>
</properties>
</project-shared-configuration>

@ -5,6 +5,8 @@
*/ */
package moe.nekojimi.chords; package moe.nekojimi.chords;
import com.amihaiemil.eoyaml.Yaml;
import com.amihaiemil.eoyaml.YamlMapping;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -31,6 +33,26 @@ class Format
this.note = note; this.note = note;
} }
public static Format fromYaml(YamlMapping yaml)
{
Format format = new Format(
yaml.string("code"),
yaml.string("extension"),
yaml.string("resolution"),
yaml.string("note"));
return format;
}
public YamlMapping toYaml()
{
return Yaml.createYamlMappingBuilder()
.add("code", code)
.add("extension", extension)
.add("resolution", resolution)
.add("note", note)
.build();
}
public boolean isAudioOnly() public boolean isAudioOnly()
{ {
return resolution.trim().toLowerCase().contains("audio only"); return resolution.trim().toLowerCase().contains("audio only");

@ -5,7 +5,12 @@
*/ */
package moe.nekojimi.chords; package moe.nekojimi.chords;
import com.amihaiemil.eoyaml.Yaml;
import com.amihaiemil.eoyaml.YamlMapping;
import com.amihaiemil.eoyaml.YamlSequence;
import com.amihaiemil.eoyaml.YamlSequenceBuilder;
import java.io.File; import java.io.File;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -32,6 +37,44 @@ public class Song
this.url = url; this.url = url;
} }
public YamlMapping toYaml()
{
YamlSequenceBuilder build = Yaml.createYamlSequenceBuilder();
for (Format format : formats)
build = build.add(format.toYaml());
return Yaml.createYamlMappingBuilder()
.add("title", title)
.add("url", url.toExternalForm())
.add("location", location.getAbsolutePath())
.add("num", Integer.toString(number))
.add("formats", build.build())
.add("requestedBy", requestedBy)
.add("requestedIn", requestedIn)
.add("kept", Boolean.toString(kept))
.build();
}
public static Song fromYaml(YamlMapping map) throws MalformedURLException
{
Song song = new Song(new URL(map.string("url")));
song.setArtist(map.string("artist"));
song.setLocation(new File(map.string("location")));
song.setNumber(map.integer("num"));
song.setKept(Boolean.parseBoolean(map.string("kept")));
song.setRequestedBy(map.string("requestedBy"));
song.setRequestedIn(map.string("requestedIn"));
List<Format> formats = new ArrayList<>();
YamlSequence formatSeq = map.yamlSequence("formats");
for (int i = 0; i < formats.size(); i++)
formats.add(Format.fromYaml(formatSeq.yamlMapping(i)));
song.setFormats(formats);
return song;
}
public String getTitle() public String getTitle()
{ {
return title; return title;
@ -148,4 +191,5 @@ public class Song
return formats.get(0); return formats.get(0);
} }
} }

Loading…
Cancel
Save