Example usage for com.google.gson FieldAttributes FieldAttributes

List of usage examples for com.google.gson FieldAttributes FieldAttributes

Introduction

In this page you can find the example usage for com.google.gson FieldAttributes FieldAttributes.

Prototype

public FieldAttributes(Field f) 

Source Link

Document

Constructs a Field Attributes object from the f .

Usage

From source file:com.intuit.wasabi.tests.model.Bucket.java

License:Apache License

/**
 * This is a workaround for the allocation percentage! If they are not equal, they are compared again
 * only taking the first 5 decimal digits into account, which should usually be more than enough.
 *
 * @param other another object/*from w w  w .j  a  v a 2 s .c  o  m*/
 * @return true if both are equal, with the exception of the allocation percentage as described above.
 */
@Override
public boolean equals(Object other) {
    if (super.equals(other))
        return true;
    LOGGER.info("Retrying bucket comparison, maybe allocation percentages have floating point inaccuracies.");
    if (!this.getClass().isInstance(other)) {
        return false;
    }

    for (Field field : this.getClass().getFields()) {
        if (!this.getSerializationStrategy().shouldSkipField(new FieldAttributes(field))) {
            try {
                boolean thisFieldEquals = Objects.equals(field.get(this), field.get(other));
                if (!thisFieldEquals && field.getName().equals("allocationPercent")) {
                    LOGGER.debug("Retrying comparison of allocation percentages: " + this.allocationPercent
                            + " and " + ((Bucket) other).allocationPercent);
                    String allocThis = String.valueOf(this.allocationPercent);
                    String allocOther = String.valueOf(((Bucket) other).allocationPercent);
                    allocThis = allocThis.substring(0,
                            Math.min(allocThis.indexOf(".") + 6, allocThis.length()));
                    allocOther = allocOther.substring(0,
                            Math.min(allocOther.indexOf(".") + 6, allocOther.length()));
                    LOGGER.debug("\tComparing only " + allocThis + " and " + allocOther);
                    thisFieldEquals = Objects.equals(allocThis, allocOther);
                    LOGGER.debug("\tResult: " + (thisFieldEquals ? "" : "not ") + "equal.");
                }
                if (!thisFieldEquals) {
                    return false;
                }
            } catch (IllegalAccessException ignored) {
                // do nothing, just skip this field
            }
        }
    }
    return true;
}

From source file:com.intuit.wasabi.tests.model.ModelItem.java

License:Apache License

/**
 * Implements an equals method to compare this instance to other objects.
 * Two instances are considered iff they are both of the same type and their
 * members are equal. That also means that members of both experiments can be
 * {@code null}, as long as the specific member is {@code null} for both instances
 * and not just one.//  w  ww.j a va2s.  c o m
 *
 * However some tests might need two instances to be equal in all but a few attributes, for example
 * two experiments can be equal except for their {@code modificationTime}.
 *
 * In that case you can specify the fields to be excluded from the equality tests by
 * setting the {@link SerializationStrategy} accordingly.
 *
 * Note that this slightly breaks the contract with the consistency of equals and hashCode!
 *
 * @param other another object
 * @return true iff both are objects are of the same type and all but their excluded member variables are equal.
 */
@Override
public boolean equals(Object other) {
    if (!this.getClass().isInstance(other)) {
        return false;
    }

    boolean equal = true;
    for (Field field : this.getClass().getFields()) {
        if (!this.getSerializationStrategy().shouldSkipField(new FieldAttributes(field))) {
            try {
                boolean thisFieldEquals = Objects.equals(field.get(this), field.get(other));
                if (!thisFieldEquals) {
                    LOGGER.debug("Field " + field.getName() + " not equal.");
                }
                equal &= thisFieldEquals;
            } catch (IllegalAccessException ignored) {
                // do nothing, just skip this field
            }
        }
    }
    return equal;
}

From source file:com.xpbytes.gson.hal.HalReflection.java

License:Apache License

/**
 * Get the type of the field as an item. Will walk collections to find the inner type.
 *
 * @see #getFieldType(Field)/* ww w.j av a  2  s. c  om*/
 * @see #getFieldType(FieldAttributes)
 * @see #getFieldItemizedType(FieldAttributes)
 *
 * @param field the field
 * @return the type
 */
static Class<?> getFieldItemizedType(Field field) {
    return getFieldItemizedType(new FieldAttributes(field));
}

From source file:com.xpbytes.gson.hal.HalReflection.java

License:Apache License

/**
 * Gets the type of a field./*w  w w  .j  av  a 2s . co  m*/
 *
 * @see #getFieldType(FieldAttributes)
 * @see #getFieldItemizedType(Field)
 * @see #getFieldItemizedType(FieldAttributes)
 *
 * @param field the field
 * @return the type
 */
static Class<?> getFieldType(Field field) {
    return getFieldType(new FieldAttributes(field));
}

From source file:net.bpiwowar.experimaestro.tasks.XPMTypeAdapterFactory.java

License:Open Source License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeAttributes attributes, TypeToken<T> type) {
    // Predefined types
    if (type.getRawType() == File.class)
        return (TypeAdapter<T>) new FileAdapter();

    // Look at registry to find a proxy for annotations
    for (Factory factory : registry) {
        if (type.getRawType().isAssignableFrom(factory.field.getType())) {
            // Found one factory, take the annotations from this field instead
            // of the original one
            attributes = new FieldAttributes(factory.field);
            break;
        }/*from  ww w  .  java2  s  . c  om*/
    }

    final ClassChooser annotation = attributes.getAnnotation(ClassChooser.class);
    if (annotation != null) {
        try {
            return new ClassChooserAdapter(gson, annotation);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error while creating class chooser adapter for " + type);
        }
    }

    return null;
}