Skip to content

Commit

Permalink
Merge pull request #160 from cmaron/master
Browse files Browse the repository at this point in the history
Use a LinkedHashSet rather than a HashSet for uniqe lists to preserve order

Closes #159.
  • Loading branch information
joelittlejohn committed Mar 5, 2014
2 parents 8a5e566 + df2cc66 commit 6b413ea
Showing 1 changed file with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@

package org.jsonschema2pojo.rules;

import static org.apache.commons.lang3.StringUtils.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.joda.time.DateTime;
import org.jsonschema2pojo.Schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.util.StdDateFormat;
Expand All @@ -40,10 +27,20 @@
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JType;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import static org.apache.commons.lang3.StringUtils.*;
import org.joda.time.DateTime;
import org.jsonschema2pojo.Schema;

/**
* Applies the "enum" schema rule.
*
*
* @see <a
* href="http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.20">http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.20</a>
*/
Expand All @@ -64,7 +61,7 @@ public DefaultRule(RuleFactory ruleFactory) {
* <p>
* Collections (Lists and Sets) are initialized to an empty collection, even
* when no default value is present in the schema (node is null).
*
*
* @param nodeName
* the name of the property which has (or may have) a default
* @param node
Expand All @@ -84,7 +81,7 @@ public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema c

if (defaultPresent && !field.type().isPrimitive() && node.isNull()) {
field.init(JExpr._null());

} else if (fieldType.startsWith(List.class.getName())) {
field.init(getDefaultList(field.type(), node));

Expand Down Expand Up @@ -151,7 +148,7 @@ private Class<?> getDateType() {
* <li>Using {@link Arrays#asList(Object...)} to initialize the list with
* the correct default values
* </ol>
*
*
* @param fieldType
* the java type that applies for this field ({@link List} with
* some generic type argument)
Expand Down Expand Up @@ -184,11 +181,11 @@ private JExpression getDefaultList(JType fieldType, JsonNode node) {
/**
* Creates a default value for a set property by:
* <ol>
* <li>Creating a new {@link HashSet} with the correct generic type
* <li>Creating a new {@link LinkedHashSet} with the correct generic type
* <li>Using {@link Arrays#asList(Object...)} to initialize the set with the
* correct default values
* </ol>
*
*
* @param fieldType
* the java type that applies for this field ({@link Set} with
* some generic type argument)
Expand All @@ -201,7 +198,7 @@ private JExpression getDefaultSet(JType fieldType, JsonNode node) {

JClass setGenericType = ((JClass) fieldType).getTypeParameters().get(0);

JClass setImplClass = fieldType.owner().ref(HashSet.class);
JClass setImplClass = fieldType.owner().ref(LinkedHashSet.class);
setImplClass = setImplClass.narrow(setGenericType);

JInvocation newSetImpl = JExpr._new(setImplClass);
Expand Down

0 comments on commit 6b413ea

Please sign in to comment.