org.simantics.databoard.util
Class Bean

java.lang.Object
  extended by org.simantics.databoard.util.Bean
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable<Bean>
Direct Known Subclasses:
Bean.Id, MyClass2, MyTestClass

public class Bean
extends java.lang.Object
implements java.lang.Cloneable, java.lang.Comparable<Bean>

Bean is a class with public fields or public get/setters. This support class adds the following services sub-classes: toString #toString() JSON #print() / #parse() Hash-Equals #hashCode() / #equals() Comparable #compareTo() Serialization #serialize()/#deserialize(), #readObject()/#writeObject(), #readFile()/#writeFile() Cloning #clone() / #readFrom() Initialization #init() / #setToDefault() / #setToRandom() Bean class must be compatible with databoard's type system. To use this class, extend it. See BeanExample for example.

Author:
toni.kalajainen

Nested Class Summary
static class Bean.Id
          In this version of the bean, the hash/equals compares to identifiers.
 
Method Summary
 void assertIsValid()
           
 Bean clone()
           
 int compareTo(Bean o)
           
 void deserialize(byte[] data)
          Deserialize the object from a byte array
 boolean equalContents(java.lang.Object obj)
           
 boolean equals(java.lang.Object obj)
          Compare to another bean of same datatype.
 RecordBinding getBinding()
          Return datatype binding to this class.
 java.lang.Object getField(java.lang.String fieldName)
          Get value of a field
 java.lang.Object getField(java.lang.String fieldName, Binding binding)
          Get value of a field
 Binding getFieldBinding(java.lang.String fieldName)
          Get binding of a field
 java.lang.Object getFieldUnchecked(java.lang.String fieldName)
          Get value of a field
 java.lang.Object getIdentifier()
          Get identifier of the object.
 Binding getIdentifierBinding()
          Get identifier binding.
 boolean hasField(java.lang.String fieldName)
           
 int hashCode()
           
 void init()
          Set default value to any null field that is not optional.
 void parse(java.lang.String str)
          Read the contents from JSON String
 java.lang.String print()
          Print the value to JSON format
 void print(java.lang.Appendable out)
          Print the value in JSON format
 java.lang.String printLine()
          Print the value to JSON format
 void readAvailableFields(Bean other)
           
 void readFile(java.io.File file)
           
 void readFrom(Bean other)
          Read all field values from another object.
 void readObject(java.io.ObjectInputStream in)
           
 byte[] serialize()
          Serialize the object to a byte array
 void setField(java.lang.String fieldName, Binding fieldBinding, java.lang.Object field)
           
 void setToDefault()
          Sets all fields to default values.
 void setToRandom(java.util.Random random)
          Sets all fields with random values
 java.lang.String toString()
          Print the object as string
 void writeFile(java.io.File file)
           
 void writeObject(java.io.ObjectOutputStream out)
           
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

getBinding

public RecordBinding getBinding()
Return datatype binding to this class.

Returns:
record binding

readFrom

public void readFrom(Bean other)
Read all field values from another object. All fields are deep-cloned, except immutable values which are referenced.

Parameters:
other -

readAvailableFields

public void readAvailableFields(Bean other)

init

public void init()
Set default value to any null field that is not optional.


setToDefault

public void setToDefault()
Sets all fields to default values. Strings are set to "", arrays cleared or set to minimum count, numbers are set to 0.


setToRandom

public void setToRandom(java.util.Random random)
Sets all fields with random values


hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

clone

public Bean clone()
Overrides:
clone in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Compare to another bean of same datatype. (Can be different binding)

Overrides:
equals in class java.lang.Object

equalContents

public boolean equalContents(java.lang.Object obj)

compareTo

public int compareTo(Bean o)
Specified by:
compareTo in interface java.lang.Comparable<Bean>

toString

public java.lang.String toString()
Print the object as string

Overrides:
toString in class java.lang.Object
Returns:
toString()

print

public void print(java.lang.Appendable out)
           throws java.io.IOException
Print the value in JSON format

Parameters:
out -
Throws:
java.io.IOException

print

public java.lang.String print()
                       throws java.io.IOException
Print the value to JSON format

Returns:
Bean in JSON string format
Throws:
java.io.IOException

printLine

public java.lang.String printLine()
                           throws java.io.IOException
Print the value to JSON format

Returns:
JSON representation in a line
Throws:
java.io.IOException

parse

public void parse(java.lang.String str)
           throws DataTypeSyntaxError
Read the contents from JSON String

Parameters:
str -
Throws:
DataTypeSyntaxError

readObject

public void readObject(java.io.ObjectInputStream in)
                throws java.lang.ClassNotFoundException,
                       java.io.IOException
Throws:
java.lang.ClassNotFoundException
java.io.IOException

writeObject

public void writeObject(java.io.ObjectOutputStream out)
                 throws java.io.IOException
Throws:
java.io.IOException

serialize

public byte[] serialize()
                 throws java.io.IOException
Serialize the object to a byte array

Returns:
bean as serialized
Throws:
java.io.IOException

deserialize

public void deserialize(byte[] data)
                 throws java.io.IOException
Deserialize the object from a byte array

Parameters:
data -
Throws:
java.io.IOException

readFile

public void readFile(java.io.File file)
              throws java.io.IOException
Throws:
java.io.IOException

writeFile

public void writeFile(java.io.File file)
               throws java.io.IOException
Throws:
java.io.IOException

assertIsValid

public void assertIsValid()
                   throws BindingException
Throws:
BindingException

setField

public void setField(java.lang.String fieldName,
                     Binding fieldBinding,
                     java.lang.Object field)
              throws BindingException
Throws:
BindingException

hasField

public boolean hasField(java.lang.String fieldName)
                 throws BindingException
Throws:
BindingException

getFieldBinding

public Binding getFieldBinding(java.lang.String fieldName)
                        throws BindingException
Get binding of a field

Parameters:
fieldName -
Returns:
binding or null of field does not exist
Throws:
BindingException

getField

public java.lang.Object getField(java.lang.String fieldName)
                          throws BindingException
Get value of a field

Parameters:
fieldName -
Returns:
value or null if field does not exist
Throws:
BindingException

getField

public java.lang.Object getField(java.lang.String fieldName,
                                 Binding binding)
                          throws BindingException
Get value of a field

Parameters:
fieldName -
binding - requested binding
Returns:
value or null if field does not exist
Throws:
BindingException

getFieldUnchecked

public java.lang.Object getFieldUnchecked(java.lang.String fieldName)
                                   throws RuntimeBindingException
Get value of a field

Parameters:
fieldName -
Returns:
value or null if field does not exist
Throws:
RuntimeBindingException

getIdentifierBinding

public Binding getIdentifierBinding()
                             throws BindingException
Get identifier binding. Use @Identifier annotation to indicate which fields compose the identifier of the record.

Returns:
idenfitier binding.
Throws:
BindingException - there is no identifier

getIdentifier

public java.lang.Object getIdentifier()
                               throws BindingException
Get identifier of the object. Use @Identifier annotation to indicate which fields compose the identifier of the record.

Returns:
identifier
Throws:
BindingException