Skip to content

Commit

Permalink
Code cleanup and maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
3breadt committed Sep 17, 2022
1 parent a784ed4 commit 35ef76e
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 70 deletions.
1 change: 1 addition & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ You can also directly convert the contained `NSObject` objects into native Java

You can create your own property list using the various constructors of the different `NSObject` classes. Or you can wrap existing native Java structures with the method `NSObject.wrap(Object o)`. Just make sure that the root object of the property list is either a `NSDictionary` (can be created from objects of the type `Map<String, Object>`) or a `NSArray` (can be created from object arrays).

For building a XML property list you can then call the `toXMLPropertyList` method on the root object of your property list. It will give you a UTF-8 `String` containing the property list in XML format.
For building an XML property list you can then call the `toXMLPropertyList` method on the root object of your property list. It will give you a UTF-8 `String` containing the property list in XML format.

If you want to have the property list in binary format use the `BinaryPropertyListWriter` class. It can write the binary property list directly to a file or to an `OutputStream`.

Expand Down Expand Up @@ -150,4 +150,4 @@ In this example your property list file is called _properties.plist_.
root.put("People", people);

//Save the propery list
PropertyListParser.saveAsXML(root, new File("people.plist"));
XMLPropertyListWriter.write(root, new File("people.plist"));
12 changes: 6 additions & 6 deletions dd-plist.iml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.9.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.9.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.9.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.9.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.9.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.9.0" level="project" />
</component>
</module>
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/dd/plist/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package com.dd.plist;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
* <p>Encodes and decodes to and from Base64 notation.</p>
* <p>Homepage: <a href="http://iharder.net/base64">http://iharder.net/base64</a>.</p>
Expand Down Expand Up @@ -1420,12 +1423,10 @@ public Class<?> resolveClass(java.io.ObjectStreamClass streamClass)

obj = ois.readObject();
} // end try
catch (java.io.IOException e) {
throw e; // Catch and throw in order to execute finally{}
} // end catch
catch (java.lang.ClassNotFoundException e) {
catch (IOException | ClassNotFoundException e) {
throw e; // Catch and throw in order to execute finally{}
} // end catch
// end catch
finally {
try {
bais.close();
Expand Down Expand Up @@ -1646,7 +1647,7 @@ public static void encodeFileToFile(String infile, String outfile)
try {
out = new java.io.BufferedOutputStream(
new java.io.FileOutputStream(outfile));
out.write(encoded.getBytes("US-ASCII")); // Strict, 7-bit output.
out.write(encoded.getBytes(StandardCharsets.US_ASCII)); // Strict, 7-bit output.
} // end try
catch (java.io.IOException e) {
throw e; // Catch and release to execute finally{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* An input stream that filters the Byte Order Mark from the input.
*/
class ByteOrderMarkFilterInputStream extends FilterInputStream {
private boolean closeStream;
private final boolean closeStream;
private boolean readingBom = true;
private final Queue<Integer> consumedBytes = new LinkedList<>();

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/dd/plist/ByteOrderMarkReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class ByteOrderMarkReader {
{ 0xFF, 0xFE, 0x00, 0x00 },
};

private static final String Charsets[] = {
private static final String[] Charsets = {
"UTF-8",
"UTF-16BE",
"UTF-16LE",
"UTF-32BE",
"UTF-32LE"
};

private boolean[] charsetPossible = { true, true, true, true, true};
private final boolean[] charsetPossible = { true, true, true, true, true};
private int offset;
private String charset;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dd/plist/NSDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
public class NSDate extends NSObject {

private Date date;
private final Date date;

// EPOCH = new SimpleDateFormat("yyyy MM dd zzz").parse("2001 01 01 GMT").getTime();
// ...but that's annoying in a static initializer because it can throw exceptions, ick.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dd/plist/NSDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class NSDictionary extends NSObject implements Map<String, NSObject> {
* Creates a new NSDictionary instance.
*/
public NSDictionary() {
this.dict = new LinkedHashMap<String, NSObject>();
this.dict = new LinkedHashMap<>();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/dd/plist/NSNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ protected void toASCII(StringBuilder ascii, int level) {
if (this.isBoolean()) {
ascii.append(this.boolValue ? "YES" : "NO");
} else {
ascii.append(this.toString());
ascii.append(this);
}
}

Expand All @@ -454,13 +454,13 @@ protected void toASCIIGnuStep(StringBuilder ascii, int level) {
switch (this.type()) {
case INTEGER: {
ascii.append("<*I");
ascii.append(this.toString());
ascii.append(this);
ascii.append('>');
break;
}
case REAL: {
ascii.append("<*R");
ascii.append(this.toString());
ascii.append(this);
ascii.append('>');
break;
}
Expand All @@ -484,10 +484,10 @@ public int compareTo(Object o) {
if (o instanceof NSNumber) {
NSNumber num = (NSNumber) o;
y = num.doubleValue();
return (x < y) ? -1 : ((x == y) ? 0 : 1);
return Double.compare(x, y);
} else if (o instanceof Number) {
y = ((Number) o).doubleValue();
return (x < y) ? -1 : ((x == y) ? 0 : 1);
return Double.compare(x, y);
} else {
return -1;
}
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/com/dd/plist/NSObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ private static boolean isSimple(Class<?> clazz) {
private static Object getInstance(Class<?> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException e) {
throw new IllegalArgumentException("Could not instantiate class " + clazz.getSimpleName());
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("Could not instantiate class " + clazz.getSimpleName());
}
}
Expand Down Expand Up @@ -332,8 +330,8 @@ private Object deserializeObject(NSDictionary payload, Class<?> clazz, Type[] ty

Object result = getInstance(clazz);

Map<String, Method> getters = new HashMap<String, Method>();
Map<String, Method> setters = new HashMap<String, Method>();
Map<String, Method> getters = new HashMap<>();
Map<String, Method> setters = new HashMap<>();
for (Method method : clazz.getMethods()) {
String name = method.getName();
if (name.startsWith("get")) {
Expand Down Expand Up @@ -371,7 +369,7 @@ private Object deserializeObject(NSDictionary payload, Class<?> clazz, Type[] ty

private HashMap<String, Object> deserializeMap() {
HashMap<String, NSObject> originalMap = ((NSDictionary)this).getHashMap();
HashMap<String, Object> clonedMap = new HashMap<String, Object>(originalMap.size());
HashMap<String, Object> clonedMap = new HashMap<>(originalMap.size());
for(String key:originalMap.keySet()) {
clonedMap.put(key, originalMap.get(key).toJavaObject());
}
Expand All @@ -384,7 +382,7 @@ private Object deserializeMap(Class<?> clazz, Type[] types, Map<String, NSObject

if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
//fallback
result = new HashMap<String, Object>();
result = new HashMap<>();
} else {
@SuppressWarnings("unchecked")
Map<String, Object> temp = (Map<String, Object>) getInstance(clazz);
Expand Down Expand Up @@ -414,9 +412,9 @@ private Object deserializeCollection(NSObject payload, Class<?> clazz, Type[] ty
if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
//try fallback
if (List.class.isAssignableFrom(clazz)) {
result = new ArrayList<Object>();
result = new ArrayList<>();
} else if (Set.class.isAssignableFrom(clazz)) {
result = new HashSet<Object>();
result = new HashSet<>();
} else {
//we fail
throw new IllegalArgumentException("Could not find a proper implementation for " + clazz.getSimpleName());
Expand Down Expand Up @@ -513,9 +511,9 @@ private Set<Object> deserializeSet() {
Set<NSObject> originalSet = ((NSSet)this).getSet();
Set<Object> clonedSet;
if(originalSet instanceof LinkedHashSet) {
clonedSet = new LinkedHashSet<Object>(originalSet.size());
clonedSet = new LinkedHashSet<>(originalSet.size());
} else {
clonedSet = new TreeSet<Object>();
clonedSet = new TreeSet<>();
}
for(NSObject o : originalSet) {
clonedSet.add(o.toJavaObject());
Expand Down Expand Up @@ -723,7 +721,7 @@ private static NSDictionary fromPojo(Object object, Class<?> objClass) {

private static NSDictionary fromMap(Map<?, ?> map) {
NSDictionary result = new NSDictionary();
for (Map.Entry entry : map.entrySet()) {
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (!(entry.getKey() instanceof String)) {
throw new IllegalArgumentException("Maps need a String key for mapping to NSDictionary.");
}
Expand Down Expand Up @@ -759,7 +757,7 @@ private static NSData fromData(Object object) {
}

private static NSArray fromCollection(Collection<?> collection) {
List<NSObject> payload = new ArrayList<NSObject>(collection.size());
List<NSObject> payload = new ArrayList<>(collection.size());
for (Object elem : collection) {
payload.add(fromJavaObject(elem));
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/dd/plist/NSSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
public class NSSet extends NSObject {

private Set<NSObject> set;
private final Set<NSObject> set;

private boolean ordered = false;

Expand All @@ -46,7 +46,7 @@ public class NSSet extends NSObject {
* @see java.util.LinkedHashSet
*/
public NSSet() {
this.set = new LinkedHashSet<NSObject>();
this.set = new LinkedHashSet<>();
}

/**
Expand All @@ -59,9 +59,9 @@ public NSSet() {
public NSSet(boolean ordered) {
this.ordered = ordered;
if (!ordered)
this.set = new LinkedHashSet<NSObject>();
this.set = new LinkedHashSet<>();
else
this.set = new TreeSet<NSObject>();
this.set = new TreeSet<>();
}

/**
Expand All @@ -71,7 +71,7 @@ public NSSet(boolean ordered) {
* @see java.util.LinkedHashSet
*/
public NSSet(NSObject... objects) {
this.set = new LinkedHashSet<NSObject>();
this.set = new LinkedHashSet<>();
this.set.addAll(Arrays.asList(objects));
}

Expand All @@ -86,9 +86,9 @@ public NSSet(NSObject... objects) {
public NSSet(boolean ordered, NSObject... objects) {
this.ordered = ordered;
if (!ordered)
this.set = new LinkedHashSet<NSObject>();
this.set = new LinkedHashSet<>();
else
this.set = new TreeSet<NSObject>();
this.set = new TreeSet<>();
this.set.addAll(Arrays.asList(objects));
}

Expand Down Expand Up @@ -220,7 +220,7 @@ public boolean equals(Object obj) {
return false;
}
final NSSet other = (NSSet) obj;
return !(this.set != other.set && (this.set == null || !this.set.equals(other.set)));
return Objects.equals(this.set, other.set);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/dd/plist/NSString.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Scanner;

Expand Down Expand Up @@ -131,7 +131,7 @@ public float floatValue() {
* a valid decimal representation of a floating-point number, 0 is returned.
*/
public double doubleValue() {
Scanner s = new Scanner(this.content.trim()).useLocale(Locale.ROOT).useDelimiter("[^0-9.+-]+");
Scanner s = new Scanner(this.content.trim()).useLocale(Locale.ROOT).useDelimiter("[^\\d.+-]+");
if(s.hasNextDouble()) {
return s.nextDouble();
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public double doubleValue() {
*/
public boolean boolValue() {
Scanner s = new Scanner(this.content.trim()).useLocale(Locale.ROOT);
return s.hasNext("([+-]?[0]*)?[YyTt1-9].*");
return s.hasNext("([+-]?0*)?[YyTt1-9].*");
}

/**
Expand Down Expand Up @@ -249,17 +249,17 @@ void toXML(StringBuilder xml, int level) {
//Make sure that the string is encoded in UTF-8 for the XML output
synchronized (NSString.class) {
if (utf8Encoder == null)
utf8Encoder = Charset.forName("UTF-8").newEncoder();
utf8Encoder = StandardCharsets.UTF_8.newEncoder();
else
utf8Encoder.reset();

try {
ByteBuffer byteBuf = utf8Encoder.encode(CharBuffer.wrap(this.content));
byte[] bytes = new byte[byteBuf.remaining()];
byteBuf.get(bytes);
this.content = new String(bytes, "UTF-8");
this.content = new String(bytes, StandardCharsets.UTF_8);
} catch (Exception ex) {
throw new RuntimeException("Could not encode the NSString into UTF-8: " + String.valueOf(ex.getMessage()));
throw new RuntimeException("Could not encode the NSString into UTF-8: " + ex.getMessage());
}
}

Expand All @@ -285,7 +285,7 @@ public void toBinary(BinaryPropertyListWriter out) throws IOException {
ByteBuffer byteBuf;
synchronized (NSString.class) {
if (asciiEncoder == null)
asciiEncoder = Charset.forName("ASCII").newEncoder();
asciiEncoder = StandardCharsets.US_ASCII.newEncoder();
else
asciiEncoder.reset();

Expand All @@ -294,7 +294,7 @@ public void toBinary(BinaryPropertyListWriter out) throws IOException {
byteBuf = asciiEncoder.encode(charBuf);
} else {
if (utf16beEncoder == null)
utf16beEncoder = Charset.forName("UTF-16BE").newEncoder();
utf16beEncoder = StandardCharsets.UTF_16BE.newEncoder();
else
utf16beEncoder.reset();

Expand Down
Loading

0 comments on commit 35ef76e

Please sign in to comment.