Example usage for org.hibernate.collection.internal PersistentSortedMap PersistentSortedMap

List of usage examples for org.hibernate.collection.internal PersistentSortedMap PersistentSortedMap

Introduction

In this page you can find the example usage for org.hibernate.collection.internal PersistentSortedMap PersistentSortedMap.

Prototype

@Deprecated
public PersistentSortedMap(SessionImplementor session, SortedMap map) 

Source Link

Document

Constructs a PersistentSortedMap.

Usage

From source file:com.erinors.hpb.server.handler.SortedMapHandler.java

License:Apache License

@Override
public Object merge(MergingContext context, Object object) {
    if (!(object instanceof SortedMap)) {
        return null;
    }//from   w w w  .j a  v  a  2  s.  c  o m

    SortedMap<Object, Object> source = (SortedMap<Object, Object>) object;
    SortedMap<Object, Object> result;

    if (source instanceof UninitializedPersistentSortedMap) {
        result = new PersistentSortedMap(context.getSessionImplementor(), source);
        context.addProcessedObject(object, result);
    } else {
        SortedMap<Object, Object> map = new TreeMap<Object, Object>(source.comparator());
        context.addProcessedObject(object, map);

        for (Map.Entry<?, ?> entry : source.entrySet()) {
            map.put(context.merge(entry.getKey()), context.merge(entry.getValue()));
        }

        result = map;
    }

    return result;
}

From source file:org.granite.hibernate4.HibernateExternalizer.java

License:Open Source License

protected PersistentCollection newHibernateCollection(AbstractExternalizablePersistentCollection value,
        Property field) {/*from   ww w . j  ava  2 s. c om*/
    final Type target = field.getType();
    final boolean initialized = value.isInitialized();
    final String metadata = value.getMetadata();
    final boolean dirty = value.isDirty();
    final boolean sorted = (SortedSet.class.isAssignableFrom(TypeUtil.classOfType(target))
            || SortedMap.class.isAssignableFrom(TypeUtil.classOfType(target)));

    Comparator<?> comparator = null;
    if (sorted && field.isAnnotationPresent(Sort.class)) {
        Sort sort = field.getAnnotation(Sort.class);
        if (sort.type() == SortType.COMPARATOR) {
            try {
                comparator = TypeUtil.newInstance(sort.comparator(), Comparator.class);
            } catch (Exception e) {
                throw new RuntimeException("Could not create instance of Comparator: " + sort.comparator());
            }
        }
    }

    PersistentCollection coll = null;
    if (value instanceof ExternalizablePersistentSet) {
        if (initialized) {
            Set<?> set = ((ExternalizablePersistentSet) value).getContentAsSet(target, comparator);
            coll = (sorted ? new PersistentSortedSet(null, (SortedSet<?>) set) : new PersistentSet(null, set));
        } else
            coll = (sorted ? new PersistentSortedSet() : new PersistentSet());
    } else if (value instanceof ExternalizablePersistentBag) {
        if (initialized) {
            List<?> bag = ((ExternalizablePersistentBag) value).getContentAsList(target);
            coll = new PersistentBag(null, bag);
        } else
            coll = new PersistentBag();
    } else if (value instanceof ExternalizablePersistentList) {
        if (initialized) {
            List<?> list = ((ExternalizablePersistentList) value).getContentAsList(target);
            coll = new PersistentList(null, list);
        } else
            coll = new PersistentList();
    } else if (value instanceof ExternalizablePersistentMap) {
        if (initialized) {
            Map<?, ?> map = ((ExternalizablePersistentMap) value).getContentAsMap(target, comparator);
            coll = (sorted ? new PersistentSortedMap(null, (SortedMap<?, ?>) map)
                    : new PersistentMap(null, map));
        } else
            coll = (sorted ? new PersistentSortedMap() : new PersistentMap());
    } else
        throw new RuntimeException("Illegal externalizable persitent class: " + value);

    if (metadata != null && serializeMetadata != SerializeMetadata.NO
            && (serializeMetadata == SerializeMetadata.YES || !initialized)) {
        String[] toks = metadata.split(":", 3);
        if (toks.length != 3)
            throw new RuntimeException("Invalid collection metadata: " + metadata);
        Serializable key = deserializeSerializable(StringUtil.hexStringToBytes(toks[0]));
        Serializable snapshot = deserializeSerializable(StringUtil.hexStringToBytes(toks[1]));
        String role = toks[2];
        coll.setSnapshot(key, role, snapshot);
    }

    if (initialized && dirty)
        coll.dirty();

    return coll;
}