~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to test/org/hibernate/test/usercollection/parameterized/DefaultableListType.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hibernate.test.usercollection.parameterized;
 
2
 
 
3
import java.util.Iterator;
 
4
import java.util.Map;
 
5
import java.util.Properties;
 
6
import java.util.List;
 
7
 
 
8
import org.hibernate.usertype.UserCollectionType;
 
9
import org.hibernate.usertype.ParameterizedType;
 
10
import org.hibernate.collection.PersistentCollection;
 
11
import org.hibernate.engine.SessionImplementor;
 
12
import org.hibernate.persister.collection.CollectionPersister;
 
13
import org.hibernate.EntityMode;
 
14
 
 
15
/**
 
16
 * Our Hibernate type-system extension for defining our specialized collection
 
17
 * contract.
 
18
 *
 
19
 * @author Holger Brands
 
20
 * @author Steve Ebersole
 
21
 */
 
22
public class DefaultableListType implements UserCollectionType, ParameterizedType {
 
23
    private String defaultValue;
 
24
 
 
25
        public Object instantiate(int anticipatedSize) {
 
26
                DefaultableListImpl list = anticipatedSize < 0 ? new DefaultableListImpl() : new DefaultableListImpl( anticipatedSize );
 
27
                list.setDefaultValue( defaultValue );
 
28
                return list;
 
29
        }
 
30
 
 
31
        public PersistentCollection instantiate(
 
32
                        SessionImplementor session,
 
33
                        CollectionPersister persister) {
 
34
                return new PersistentDefaultableList( session );
 
35
        }
 
36
 
 
37
        public PersistentCollection wrap(SessionImplementor session, Object collection) {
 
38
                if ( session.getEntityMode() == EntityMode.DOM4J ) {
 
39
                        throw new IllegalStateException( "dom4j not supported" );
 
40
                }
 
41
                else {
 
42
                        return new PersistentDefaultableList( session, ( List ) collection );
 
43
                }
 
44
        }
 
45
 
 
46
        public Iterator getElementsIterator(Object collection) {
 
47
                return ( ( DefaultableList ) collection ).iterator();
 
48
        }
 
49
 
 
50
        public boolean contains(Object collection, Object entity) {
 
51
                return ( ( DefaultableList ) collection ).contains( entity );
 
52
        }
 
53
 
 
54
        public Object indexOf(Object collection, Object entity) {
 
55
                int index = ( ( DefaultableList ) collection ).indexOf( entity );
 
56
                return index >= 0 ? new Integer( index ) : null;
 
57
        }
 
58
 
 
59
        public Object replaceElements(
 
60
                        Object original,
 
61
                        Object target,
 
62
                        CollectionPersister persister, 
 
63
                        Object owner,
 
64
                        Map copyCache,
 
65
                        SessionImplementor session) {
 
66
                DefaultableList result = ( DefaultableList ) target;
 
67
                result.clear();
 
68
                result.addAll( ( DefaultableList ) original );
 
69
                return result;
 
70
        }
 
71
 
 
72
        public void setParameterValues(Properties parameters) {
 
73
        defaultValue = parameters.getProperty( "default" );
 
74
        }
 
75
}