001: //$Id: PersistentSortedSet.java 7714 2005-08-01 16:29:33Z oneovthafew $
002: package org.hibernate.collection;
003:
004: import java.io.Serializable;
005: import java.util.Comparator;
006: import java.util.Iterator;
007: import java.util.SortedSet;
008: import java.util.TreeMap;
009:
010: import org.hibernate.EntityMode;
011: import org.hibernate.HibernateException;
012: import org.hibernate.engine.SessionImplementor;
013: import org.hibernate.persister.collection.BasicCollectionPersister;
014:
015: /**
016: * A persistent wrapper for a <tt>java.util.SortedSet</tt>. Underlying
017: * collection is a <tt>TreeSet</tt>.
018: *
019: * @see java.util.TreeSet
020: * @author <a href="mailto:doug.currie@alum.mit.edu">e</a>
021: */
022: public class PersistentSortedSet extends PersistentSet implements
023: SortedSet {
024:
025: protected Comparator comparator;
026:
027: protected Serializable snapshot(BasicCollectionPersister persister,
028: EntityMode entityMode) throws HibernateException {
029: //if (set==null) return new Set(session);
030: TreeMap clonedSet = new TreeMap(comparator);
031: Iterator iter = set.iterator();
032: while (iter.hasNext()) {
033: Object copy = persister.getElementType().deepCopy(
034: iter.next(), entityMode, persister.getFactory());
035: clonedSet.put(copy, copy);
036: }
037: return clonedSet;
038: }
039:
040: public void setComparator(Comparator comparator) {
041: this .comparator = comparator;
042: }
043:
044: public PersistentSortedSet(SessionImplementor session) {
045: super (session);
046: }
047:
048: public PersistentSortedSet(SessionImplementor session, SortedSet set) {
049: super (session, set);
050: comparator = set.comparator();
051: }
052:
053: public PersistentSortedSet() {
054: } //needed for SOAP libraries, etc
055:
056: /**
057: * @see PersistentSortedSet#comparator()
058: */
059: public Comparator comparator() {
060: return comparator;
061: }
062:
063: /**
064: * @see PersistentSortedSet#subSet(Object,Object)
065: */
066: public SortedSet subSet(Object fromElement, Object toElement) {
067: read();
068: SortedSet s;
069: s = ((SortedSet) set).subSet(fromElement, toElement);
070: return new SubSetProxy(s);
071: }
072:
073: /**
074: * @see PersistentSortedSet#headSet(Object)
075: */
076: public SortedSet headSet(Object toElement) {
077: read();
078: SortedSet s = ((SortedSet) set).headSet(toElement);
079: return new SubSetProxy(s);
080: }
081:
082: /**
083: * @see PersistentSortedSet#tailSet(Object)
084: */
085: public SortedSet tailSet(Object fromElement) {
086: read();
087: SortedSet s = ((SortedSet) set).tailSet(fromElement);
088: return new SubSetProxy(s);
089: }
090:
091: /**
092: * @see PersistentSortedSet#first()
093: */
094: public Object first() {
095: read();
096: return ((SortedSet) set).first();
097: }
098:
099: /**
100: * @see PersistentSortedSet#last()
101: */
102: public Object last() {
103: read();
104: return ((SortedSet) set).last();
105: }
106:
107: /** wrapper for subSets to propagate write to its backing set */
108: class SubSetProxy extends SetProxy implements SortedSet {
109:
110: SubSetProxy(SortedSet s) {
111: super (s);
112: }
113:
114: public Comparator comparator() {
115: return ((SortedSet) this .set).comparator();
116: }
117:
118: public Object first() {
119: return ((SortedSet) this .set).first();
120: }
121:
122: public SortedSet headSet(Object toValue) {
123: return new SubSetProxy(((SortedSet) this .set)
124: .headSet(toValue));
125: }
126:
127: public Object last() {
128: return ((SortedSet) this .set).last();
129: }
130:
131: public SortedSet subSet(Object fromValue, Object toValue) {
132: return new SubSetProxy(((SortedSet) this .set).subSet(
133: fromValue, toValue));
134: }
135:
136: public SortedSet tailSet(Object fromValue) {
137: return new SubSetProxy(((SortedSet) this.set)
138: .tailSet(fromValue));
139: }
140:
141: }
142:
143: }
|