Top Description Inners Fields Constructors Methods
com.amazon.ion.util

public final Class Equivalence

extends Object
Class Inheritance
Static Imports
com.amazon.ion.impl._Private_IonConstants.UNKNOWN_SYMBOL_TEXT_PREFIX

Provides equivalence comparisons between two IonValues, following the contract of IonValue#equals(Object).

Basic usage of this class is as follows:

    IonValue v1 = ...;
    IonValue v2 = ...;
    com.amazon.ion.util.Equivalence.ionEquals( v1, v2 );
More likely, a static import would make using this class easier.
    import static com.amazon.ion.util.Equivalence.ionEquals;
    ...
    boolean equivalent = ionEquals( v1, v2 );

Additional options are available by configuring an Equivalence instance using Equivalence.Builder. For example:

     com.amazon.ion.util.Equivalence equivalence =
         new com.amazon.ion.util.Equivalence.Builder()
             .withEpsilon(1e-6)
             .build();
     IonValue v1 = ...;
     IonValue v2 = ...;
     equivalence.ionValueEquals( v1, v2 );
 

Ion Equivalence

In order to make Ion a useful model to describe data, we must first define the notion of equivalence for all values representable in Ion. Equivalence with respect to Ion values means that if two Ion values, X and Y, are equivalent, they represent the same data and can be substituted for the other without loss of information. This relationship is:

Ordered Sequence Equivalence

When an ordered sequence (i.e. tuple) of elements is specified in this document, equivalence over such an ordered sequence is defined as follows. A tuple, A = (a1, a2, ..., an), is equivalent to another tuple, B = (b1, b2, ..., bm) if and only if the cardinality (number of elements) of A equals the cardinality of B (i.e. n == m) and ai is equivalent to bi for i = 1 ... n.

Un-Ordered Sequence Equivalence

When an un-ordered sequence (i.e. bag or multi-set) is specified in this document, equivalence over such a sequence is defined as follows. A bag, A = {a1, a2, ..., an} is equivalent to B = {b1, b2, ..., bm} if and only if the cardinality of A is equal to the cardinality of B and for each element, x, in A there exists a distinct element, y, in B for which x is equivalent to y.

Values

Any arbitrary, atomic value in the Ion Language can be denoted as the tuple, (A, V), where A is an ordered list of annotations, and V is an Ion Primitive Data or Ion Complex Data value. The list of annotations, A is an tuple of Ion Symbols (a specific type of Ion Primitive).

Terminology

Within this class, strict equivalence refers to Ion data model equivalence as defined above and by the Ion Specification. Structural or non-strict equivalence follows the same rules as strict equivalence, except that

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Equivalence.Builder

Constructs Equivalence instances.

pack-priv static class
Equivalence.Configuration

Contains the configuration to use when comparing Ion values.

pack-priv static class
Equivalence.Field

Class that denotes a name/value pair in Structs.

Field Summary

Modifier and TypeField and Description
private final Equivalence.Configuration
private static final Equivalence.Configuration
NON_STRICT_CONFIGURATION

Configuration that requires structural equivalence without considering annotations.

private static final boolean
PUBLIC_COMPARISON_API

TODO amazon-ion/ion-java/issues/26 Marker for code that needs to be altered in order to support a public comparison API to determine ordering of values, not just equality.

private static final Equivalence.Configuration
STRICT_CONFIGURATION

Configuration that requires strict data equivalence.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static int
private static int
compareLobContents(final IonLob lob1, final IonLob lob2)

Compare LOB content by stream--assuming non-null.

private static int
compareSequences(final IonSequence s1, final IonSequence s2, final Equivalence.Configuration configuration, final int depth)

private static int
compareStructs(final IonStruct s1, final IonStruct s2, final Equivalence.Configuration configuration, final int depth)

private static int
private static final Map<Equivalence.Field, Equivalence.Field>
convertToMultiSet(final IonStruct struct, final Equivalence.Configuration configuration, final int depth)

Converts an IonStruct to a multi-set for use in IonStruct equality checks.

private static int
ionCompareToImpl(final IonValue v1, final IonValue v2, final Equivalence.Configuration configuration, final int depth)

public static boolean

Returns:

true if two Ion Values represent the same data.
ionEquals
(final IonValue
The first Ion value to compare.
v1
,
final IonValue
The second Ion value to compare.
v2
)

Checks for strict data equivalence over two Ion Values.

public static boolean

Returns:

true if two Ion Values represent the same data without regard to annotations.
ionEqualsByContent
(final IonValue
The first Ion value to compare.
v1
,
final IonValue
The second Ion value to compare.
v2
)

Checks for structural data equivalence over two Ion Values.

private static boolean
ionEqualsImpl(final IonValue v1, final IonValue v2, final Equivalence.Configuration configuration, final int depth)

public boolean

Returns:

true if two Ion Values represent the same data.
ionValueEquals
(final IonValue
The first Ion value to compare.
v1
,
final IonValue
The second Ion value to compare.
v2
)

Checks for data equivalence over two Ion values using this Equivalence's configuration

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

configurationback to summary
private final Equivalence.Configuration configuration
NON_STRICT_CONFIGURATIONback to summary
private static final Equivalence.Configuration NON_STRICT_CONFIGURATION

Configuration that requires structural equivalence without considering annotations.

See Also
ionEqualsByContent(IonValue, IonValue)
PUBLIC_COMPARISON_APIback to summary
private static final boolean PUBLIC_COMPARISON_API

TODO amazon-ion/ion-java/issues/26 Marker for code that needs to be altered in order to support a public comparison API to determine ordering of values, not just equality.

STRICT_CONFIGURATIONback to summary
private static final Equivalence.Configuration STRICT_CONFIGURATION

Configuration that requires strict data equivalence.

See Also
ionEquals(IonValue, IonValue)

Constructor Detail

Equivalenceback to summary
private Equivalence(Equivalence.Configuration configuration)

Method Detail

compareAnnotationsback to summary
private static int compareAnnotations(SymbolToken[] ann1, SymbolToken[] ann2)
compareLobContentsback to summary
private static int compareLobContents(final IonLob lob1, final IonLob lob2)

Compare LOB content by stream--assuming non-null.

compareSequencesback to summary
private static int compareSequences(final IonSequence s1, final IonSequence s2, final Equivalence.Configuration configuration, final int depth)
compareStructsback to summary
private static int compareStructs(final IonStruct s1, final IonStruct s2, final Equivalence.Configuration configuration, final int depth)
compareSymbolTokensback to summary
private static int compareSymbolTokens(SymbolToken tok1, SymbolToken tok2)
convertToMultiSetback to summary
private static final Map<Equivalence.Field, Equivalence.Field> convertToMultiSet(final IonStruct struct, final Equivalence.Configuration configuration, final int depth)

Converts an IonStruct to a multi-set for use in IonStruct equality checks. This method returns the multi-set as a Map<Field, Field>.

A multi-set supports order-independent equality, and may have duplicate elements.

Take special note that Set is missing a get() API, and cannot contain duplicate elements, hence we cannot use it.

ionCompareToImplback to summary
private static int ionCompareToImpl(final IonValue v1, final IonValue v2, final Equivalence.Configuration configuration, final int depth)
ionEqualsback to summary
public static boolean ionEquals(final IonValue v1, final IonValue v2)

Checks for strict data equivalence over two Ion Values.

Parameters
v1:IonValue

The first Ion value to compare.

v2:IonValue

The second Ion value to compare.

Returns:boolean

true if two Ion Values represent the same data.

ionEqualsByContentback to summary
public static boolean ionEqualsByContent(final IonValue v1, final IonValue v2)

Checks for structural data equivalence over two Ion Values. That is, equivalence without considering any annotations.

Parameters
v1:IonValue

The first Ion value to compare.

v2:IonValue

The second Ion value to compare.

Returns:boolean

true if two Ion Values represent the same data without regard to annotations.

ionEqualsImplback to summary
private static boolean ionEqualsImpl(final IonValue v1, final IonValue v2, final Equivalence.Configuration configuration, final int depth)
ionValueEqualsback to summary
public boolean ionValueEquals(final IonValue v1, final IonValue v2)

Checks for data equivalence over two Ion values using this Equivalence's configuration

Parameters
v1:IonValue

The first Ion value to compare.

v2:IonValue

The second Ion value to compare.

Returns:boolean

true if two Ion Values represent the same data.

See Also
Builder
com.amazon.ion.util back to summary

public final Class Equivalence.Builder

extends Object
Class Inheritance

Constructs Equivalence instances. To compare IonValues using an Equivalence instance's configuration, call ionValueEquals(IonValue, IonValue) on that instance rather than calling IonValue#equals(Object).

Field Summary

Modifier and TypeField and Description
private Double
private boolean
private int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Equivalence

Returns:

a new Equivalence using this builder's configuration.
build
()

public Equivalence.Builder

Returns:

this builder.
withEpsilon
(double
the value.
epsilon
)

The maximum absolute difference between two Ion float values for which the two values will be considered equivalent.

public Equivalence.Builder

Returns:

this builder.
withMaxComparisonDepth
(int
the value.
maxComparisonDepth
)

The maximum depth to which two values will be compared.

public Equivalence.Builder

Returns:

this builder.
withStrict
(boolean
the value.
isStrict
)

When true, checks for strict data equivalence over two Ion Values.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

epsilonback to summary
private Double epsilon
isStrictback to summary
private boolean isStrict
maxComparisonDepthback to summary
private int maxComparisonDepth

Constructor Detail

Builderback to summary
public Builder()

Method Detail

buildback to summary
public Equivalence build()
Returns:Equivalence

a new Equivalence using this builder's configuration.

withEpsilonback to summary
public Equivalence.Builder withEpsilon(double epsilon)

The maximum absolute difference between two Ion float values for which the two values will be considered equivalent. Default: Ion float values will only be considered equivalent when Double.compare(a, b) == 0.

Parameters
epsilon:double

the value.

Returns:Equivalence.Builder

this builder.

withMaxComparisonDepthback to summary
public Equivalence.Builder withMaxComparisonDepth(int maxComparisonDepth)

The maximum depth to which two values will be compared. If the two values to be compared are still potentially equivalent beyond this depth, Equivalence#ionValueEquals(IonValue, IonValue) will throw an IonException.

Parameters
maxComparisonDepth:int

the value.

Returns:Equivalence.Builder

this builder.

withStrictback to summary
public Equivalence.Builder withStrict(boolean isStrict)

When true, checks for strict data equivalence over two Ion Values. When false, checks for structural data equivalence over two Ion Values. See Equivalence for discussion of the differences between the two. Default: true.

Parameters
isStrict:boolean

the value.

Returns:Equivalence.Builder

this builder.

com.amazon.ion.util back to summary

pack-priv final Class Equivalence.Configuration

extends Object
Class Inheritance

Contains the configuration to use when comparing Ion values.

Field Summary

Modifier and TypeField and Description
private final Double
private final boolean
private final int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

epsilonback to summary
private final Double epsilon
isStrictback to summary
private final boolean isStrict
maxComparisonDepthback to summary
private final int maxComparisonDepth

Constructor Detail

Configurationback to summary
pack-priv Configuration(Equivalence.Builder builder)
com.amazon.ion.util back to summary

pack-priv Class Equivalence.Field

extends Object
Class Inheritance

Class that denotes a name/value pair in Structs.

Structs are unordered collections of name/value pairs. The names are symbol tokens, and the values are unrestricted. Each name/value pair is called a field.

The responsibilities of this class is to expose a Map<Field, Field> as a multi-set. A Field instance holds the name and value of a Struct field, and counts the number of times that name/value pair occurs within the multi-set.

For example, an IonStruct:

  {
    a : 123,
    a : 123
  }
will be converted into a multi-set Map<Field, Field> with a single Field -> Field with occurrences of 2.

Refer to Equivalence#convertToMultiSet(IonStruct, Configuration, int) and Field#equals(Object) for more info.

Note

This class should only be instantiated for the sole purpose of using it as either a key or value in a Map.

Field Summary

Modifier and TypeField and Description
private final Equivalence.Configuration
private final int
private final String
private int
occurrences

Number of times that this specific field (with the same name and value) occurs within the struct.

private final IonValue

Constructor Summary

AccessConstructor and Description
pack-priv
Field(final IonValue value, final Equivalence.Configuration configuration, final int depth)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(final Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

This method is implicitly called by Map#get(Object) to obtain a value to which other is mapped.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

configurationback to summary
private final Equivalence.Configuration configuration
depthback to summary
private final int depth
nameback to summary
private final String name
occurrencesback to summary
private int occurrences

Number of times that this specific field (with the same name and value) occurs within the struct.

valueback to summary
private final IonValue value

Constructor Detail

Fieldback to summary
pack-priv Field(final IonValue value, final Equivalence.Configuration configuration, final int depth)

Method Detail

equalsback to summary
public boolean equals(final Object other)

Overrides java.lang.Object.equals.

This method is implicitly called by Map#get(Object) to obtain a value to which other is mapped.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override