Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.persist.raw;
  
 import java.util.Map;
 
A raw instance that can be used with a RawStore or com.sleepycat.persist.evolve.Conversion. A RawObject is used to represent instances of complex types (persistent classes with fields), arrays, and enum values. It is not used to represent non-enum simple types, which are represented as simple objects. This includes primitives, which are represented as instances of their wrapper class.

RawObject objects are thread-safe. Multiple threads may safely call the methods of a shared RawObject object.

Author(s):
Mark Hayes
 
 public class RawObject {
 
     private static final String INDENT = "  ";
 
     private RawType type;
     private Map<StringObjectvalues;
     private Object[] elements;
     private String enumConstant;
     private RawObject superObject;

    
Creates a raw object with a given set of field values for a complex type.

Parameters:
type the type of this raw object.
values a map of field name to value for each declared field in the class, or null to create an empty map. Each value in the map is a RawObject, a href="../model/Entity.html.simpleTypes">simple type instance, or null.
superObject the instance of the superclass, or null if the superclass is Object.
Throws:
java.lang.IllegalArgumentException if the type argument is an array type.
 
     public RawObject(RawType type,
                      Map<StringObjectvalues,
                      RawObject superObject) {
         if (type == null || values == null) {
             throw new NullPointerException();
         }
         this. = type;
         this. = values;
         this. = superObject;
     }

    
Creates a raw object with the given array elements for an array type.

Parameters:
type the type of this raw object.
elements an array of elements. Each element in the array is a RawObject, a href="../model/Entity.html.simpleTypes">simple type instance, or null.
Throws:
java.lang.IllegalArgumentException if the type argument is not an array type.
 
     public RawObject(RawType typeObject[] elements) {
         if (type == null || elements == null) {
             throw new NullPointerException();
         }
         this. = type;
         this. = elements;
     }

    
Creates a raw object with the given enum value for an enum type.

Parameters:
type the type of this raw object.
enumConstant the String value of this enum constant; must be one of the Strings returned by RawType.getEnumConstants().
Throws:
java.lang.IllegalArgumentException if the type argument is not an array type.
 
    public RawObject(RawType typeString enumConstant) {
        if (type == null || enumConstant == null) {
            throw new NullPointerException();
        }
        this. = type;
        this. = enumConstant;
    }

    
Returns the raw type information for this raw object.

Note that if this object is unevolved, the returned type may be different from the current type returned by com.sleepycat.persist.model.EntityModel.getRawType(java.lang.String) for the same class name. This can only occur in a Conversion.convert.

    public RawType getType() {
        return ;
    }

    
Returns a map of field name to value for a complex type, or null for an array type or an enum type. The map contains a String key for each declared field in the class. Each value in the map is a RawObject, a href="../model/Entity.html.simpleTypes">simple type instance, or null.

There will be an entry in the map for every field declared in this type, as determined by RawType.getFields() for the type returned by getType(). Values in the map may be null for fields with non-primitive types.

    public Map<StringObjectgetValues() {
        return ;
    }

    
Returns the array of elements for an array type, or null for a complex type or an enum type. Each element in the array is a RawObject, a href="../model/Entity.html.simpleTypes">simple type instance, or null.
    public Object[] getElements() {
        return ;
    }

    
Returns the enum constant String for an enum type, or null for a complex type or an array type. The String returned will be one of the Strings returned by RawType.getEnumConstants().
    public String getEnum() {
        return ;
    }

    
Returns the instance of the superclass, or null if the superclass is Object or Enum.
    public RawObject getSuper() {
        return ;
    }
    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if (!(other instanceof RawObject)) {
            return false;
        }
        RawObject o = (RawObjectother;
        if ( != o.type) {
            return false;
        }
        if (!Arrays.deepEquals(o.elements)) {
            return false;
        }
        if ( != null) {
            if (!.equals(o.enumConstant)) {
                return false;
            }
        } else {
            if (o.enumConstant != null) {
                return false;
            }
        }
        if ( != null) {
            if (!.equals(o.values)) {
                return false;
            }
        } else {
            if (o.values != null) {
                return false;
            }
        }
        if ( != null) {
            if (!.equals(o.superObject)) {
                return false;
            }
        } else {
            if (o.superObject != null) {
                return false;
            }
        }
        return true;
    }
    @Override
    public int hashCode() {
        return System.identityHashCode() +
               Arrays.deepHashCode() +
               ( != null ? .hashCode() : 0) +
               ( != null ? .hashCode() : 0) +
               ( != null ? .hashCode() : 0);
    }

    
Returns an XML representation of the raw object.
    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder(500);
        formatRawObject(buf""nullfalse);
        return buf.toString();
    }
    private void formatRawObject(StringBuilder buf,
                                 String indent,
                                 String id,
                                 boolean isSuper) {
        if (.isEnum()) {
            buf.append(indent);
            buf.append("<Enum");
            formatId(bufid);
            buf.append(" class=\"");
            buf.append(.getClassName());
            buf.append("\" typeId=\"");
            buf.append(.getId());
            buf.append("\">");
            buf.append();
            buf.append("</Enum>\n");
        } else {
            String indent2 = indent + ;
            String endTag;
            buf.append(indent);
            if (.isArray()) {
                buf.append("<Array");
                endTag = "</Array>";
            } else if (isSuper) {
                buf.append("<Super");
                endTag = "</Super>";
            } else {
                buf.append("<Object");
                endTag = "</Object>";
            }
            formatId(bufid);
            if (.isArray()) {
                buf.append(" length=\"");
                buf.append(.);
                buf.append('"');
            }
            buf.append(" class=\"");
            buf.append(.getClassName());
            buf.append("\" typeId=\"");
            buf.append(.getId());
            buf.append("\">\n");
            if ( != null) {
                .formatRawObject(bufindent2nulltrue);
            }
            if (.isArray()) {
                for (int i = 0; i < .i += 1) {
                    formatValue(bufindent2, String.valueOf(i), [i]);
                }
            } else {
                TreeSet<Stringkeys = new TreeSet<String>(.keySet());
                for (String name : keys) {
                    formatValue(bufindent2name.get(name));
                }
            }
            buf.append(indent);
            buf.append(endTag);
            buf.append("\n");
        }
    }
    private static void formatValue(StringBuilder buf,
                                    String indent,
                                    String id,
                                    Object val) {
        if (val == null) {
            buf.append(indent);
            buf.append("<Null");
            formatId(bufid);
            buf.append("/>\n");
        } else if (val instanceof RawObject) {
            ((RawObjectval).formatRawObject(bufindentidfalse);
        } else {
            buf.append(indent);
            buf.append("<Value");
            formatId(bufid);
            buf.append(" class=\"");
            buf.append(val.getClass().getName());
            buf.append("\">");
            buf.append(val.toString());
            buf.append("</Value>\n");
        }
    }
    private static void formatId(StringBuilder bufString id) {
        if (id != null) {
            if (Character.isDigit(id.charAt(0))) {
                buf.append(" index=\"");
            } else {
                buf.append(" field=\"");
            }
            buf.append(id);
            buf.append('"');
        }
    }
New to GrepCode? Check out our FAQ X