Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a LinkedHashSet rather than a HashSet for uniqe lists to preserve order #160

Merged
merged 1 commit into from
Mar 5, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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