Databoard Tutorials: Difference between revisions

From Developer Documents
Jump to navigation Jump to search
m Created page with "This document contains a some Java coding snippets for Databoard. # Object Deep-Copy The binding class can be used for cloning objects. The copies are <syntaxHighlight lang="ja..."
 
mNo edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
This document contains a some Java coding snippets for Databoard.
This document contains a some Java coding snippets for Databoard.


# Object Deep-Copy
==Object Deep-Copy==
The binding class can be used for cloning objects. The copies are  
The binding class can be used for cloning objects. All copied objects are deep. Mutable objects are duplicated, and immutable are referenced.
 
<syntaxHighlight lang="java">
// Binding can copy objects. All copies are deep.
// Mutable objects are duplicated, immutable are referenced.
// For instance, in object array, the array is copied, but its
// immutable literal instances (e.g. 1) is referenced.
Object[] original = new Object[] { 1, "X", 123.456 };
Binding binding = Bindings.getBinding( original.getClass() );
Object[] copy = (Object[]) binding.cloneUnchecked( original );
 
// Print the objects
System.out.println( "Original: "+binding.toString( original ) );
System.out.println( "Clone  : "+binding.toString( copy    ) );   
</syntaxHighlight>
 
Recursive classes can also be cloned, but they must be annotated with @Referable tag.
<syntaxHighlight lang="java">
static @Referable class X {
public X reference;
}
public static void main(String[] args) throws BindingConstructionException, BindingException {
// Create recursive instance
X original = new X();
original.reference = original;
Binding binding = Bindings.getBinding( original.getClass() );
X copy = (X) binding.cloneUnchecked( original );
 
// Print the objects
System.out.println( "Original: "+binding.toString( original ) );
System.out.println( "Clone  : "+binding.toString( copy    ) );
}
</syntaxHighlight>
 
==Object Deep-Compare==
Binding can compare any two instances. The compare function is deep.
 
<syntaxHighlight lang="java">
<syntaxHighlight lang="java">
// Binding can copy objects. All copies are deep.
Binding binding = Bindings.getBindingUnchecked( int[].class );
// Mutable objects are duplicated, immutable are referenced.
// For instance, in object array, the array is copied, but its
int[] array1 = new int[] { 1, 2, 3 };
// immutable literal instances (e.g. 1) is referenced.
int[] array2 = new int[] { 1, 2, 3 };
int[] array3 = new int[] { 2, 3, 4 };
Object[] original = new Object[] { new Object(), 1, "X", 123.456 };
if ( binding.compare(array1, array2) == 0 ) {
Binding binding = Bindings.getBinding( original.getClass() );
System.out.println( "array1 is equal to array2" );
Object[] copy = (Object[]) binding.cloneUnchecked( original );
} else {
System.out.println( "array1 is not equal to array2" );
}
if ( binding.compare(array1, array3) == 0 ) {
System.out.println( "array1 is equal to array3" );
} else {
System.out.println( "array1 is not equal to array3" );
}
</syntaxHighlight>
 


// Print the objects
Two bindings can compare instances of same datatype, even if they are of different classes.
System.out.println( "Original: "+binding.toString( original ) );
<syntaxHighlight lang="java">
System.out.println( "Clone  : "+binding.toString( copy    ) );  
ArrayList<Integer> array4 = new ArrayList<Integer>();
Binding binding2 = Bindings.getBindingUnchecked( ArrayList.class, Integer.class );
array4.add( 1 );
array4.add( 2 );
array4.add( 3 );
if ( Bindings.compare(binding, array1, binding2, array4) == 0 ) {
System.out.println( "array1 is equal to array4" );
} else {
System.out.println( "array1 is not equal to array4" );
}
</syntaxHighlight>
</syntaxHighlight>


 
 
== Object deep-hashcode ==
# Object Deep-Compare
Binding can calculate deep hashcode for any instance
 
<syntaxHighlight lang="java">
Binding binding = Bindings.getBindingUnchecked( int[].class );
int[] array1 = new int[] { 1, 2, 3 };
int[] array2 = new int[] { 1, 2, 3 };
int[] array3 = new int[] { 2, 3, 4 };
System.out.println( "Hashcode for array1 is " + binding.hashValue( array1 ) );
System.out.println( "Hashcode for array2 is " + binding.hashValue( array2 ) );
System.out.println( "Hashcode for array3 is " + binding.hashValue( array3 ) );
</syntaxHighlight>

Latest revision as of 11:17, 7 September 2011

This document contains a some Java coding snippets for Databoard.

Object Deep-Copy

The binding class can be used for cloning objects. All copied objects are deep. Mutable objects are duplicated, and immutable are referenced.

<syntaxHighlight lang="java"> // Binding can copy objects. All copies are deep. // Mutable objects are duplicated, immutable are referenced.

// For instance, in object array, the array is copied, but its // immutable literal instances (e.g. 1) is referenced.

Object[] original = new Object[] { 1, "X", 123.456 }; Binding binding = Bindings.getBinding( original.getClass() ); Object[] copy = (Object[]) binding.cloneUnchecked( original );

// Print the objects System.out.println( "Original: "+binding.toString( original ) ); System.out.println( "Clone  : "+binding.toString( copy ) ); </syntaxHighlight>

Recursive classes can also be cloned, but they must be annotated with @Referable tag. <syntaxHighlight lang="java"> static @Referable class X { public X reference; }

public static void main(String[] args) throws BindingConstructionException, BindingException { // Create recursive instance X original = new X(); original.reference = original;

Binding binding = Bindings.getBinding( original.getClass() ); X copy = (X) binding.cloneUnchecked( original );

// Print the objects System.out.println( "Original: "+binding.toString( original ) ); System.out.println( "Clone  : "+binding.toString( copy ) ); } </syntaxHighlight>

Object Deep-Compare

Binding can compare any two instances. The compare function is deep.

<syntaxHighlight lang="java"> Binding binding = Bindings.getBindingUnchecked( int[].class );

int[] array1 = new int[] { 1, 2, 3 }; int[] array2 = new int[] { 1, 2, 3 }; int[] array3 = new int[] { 2, 3, 4 };

if ( binding.compare(array1, array2) == 0 ) { System.out.println( "array1 is equal to array2" ); } else { System.out.println( "array1 is not equal to array2" ); }

if ( binding.compare(array1, array3) == 0 ) { System.out.println( "array1 is equal to array3" ); } else { System.out.println( "array1 is not equal to array3" ); } </syntaxHighlight>


Two bindings can compare instances of same datatype, even if they are of different classes. <syntaxHighlight lang="java"> ArrayList<Integer> array4 = new ArrayList<Integer>(); Binding binding2 = Bindings.getBindingUnchecked( ArrayList.class, Integer.class ); array4.add( 1 ); array4.add( 2 ); array4.add( 3 );

if ( Bindings.compare(binding, array1, binding2, array4) == 0 ) { System.out.println( "array1 is equal to array4" ); } else { System.out.println( "array1 is not equal to array4" ); } </syntaxHighlight>


Object deep-hashcode

Binding can calculate deep hashcode for any instance

<syntaxHighlight lang="java"> Binding binding = Bindings.getBindingUnchecked( int[].class );

int[] array1 = new int[] { 1, 2, 3 }; int[] array2 = new int[] { 1, 2, 3 }; int[] array3 = new int[] { 2, 3, 4 };

System.out.println( "Hashcode for array1 is " + binding.hashValue( array1 ) ); System.out.println( "Hashcode for array2 is " + binding.hashValue( array2 ) ); System.out.println( "Hashcode for array3 is " + binding.hashValue( array3 ) ); </syntaxHighlight>