summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/collection/Collection.java
blob: 270d021a27492adfa3f5c161fe12226345e9c57b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.c2kernel.collection;

import java.util.Iterator;

import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.lookup.ItemPath;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.utils.CastorHashMap;

/**
 * @version $Revision: 1.23 $ $Date: 2004/05/14 15:39:39 $
 * @author  $Author: abranson $
 */
abstract public class Collection<E extends CollectionMember> implements C2KLocalObject
{

	public static final short EMPTY = -1;
    private int mCounter = -1;      // Contains next available Member ID
    protected CollectionMemberList<E> mMembers = new CollectionMemberList<E>();
    protected int mID = -1;
    protected String mName = "";     // Not checked for uniqueness
    protected Integer mVersion = null;

	public int getCounter()
    {
        if (mCounter == -1)
			for (E element : mMembers.list) {
            if (mCounter < element.getID())
                mCounter = element.getID();
        }
        return ++mCounter;
    }

	public void setCounter(int count)
    {
        mCounter = count;
    }


	public int size()
    {
        return mMembers.list.size();
    }

    public void setID(int id)
    {
        mID = id;
    }

    public int getID()
    {
        return mID;
    }

	@Override
	public void setName(String name)
    {
        mName = name;
    }

	@Override
	public String getName()
    {
        return mName;
    }
	
	public Integer getVersion() {
		return mVersion;
	}

	public void setVersion(Integer mVersion) {
		this.mVersion = mVersion;
	}

	public String getVersionName() {
		return mVersion==null?"last":String.valueOf(mVersion);
	}
	@Override
	public String getClusterType()
    {
        return ClusterStorage.COLLECTION;
    }

	public void setMembers(CollectionMemberList<E> newMembers)
    {
    	mMembers = newMembers;
    }

    public boolean contains(ItemPath itemPath) {
        for (E element : mMembers.list) {
            if (element.getItemPath().equals(itemPath))
                return true;
        }
        return false;
    }
    
    public String getDescVer(E mem) {
    	String descVer = "last";
    	Object descVerObj = mem.getProperties().get("Version");
    	if (descVerObj != null) descVer = descVerObj.toString();
    	return descVer;
    }

    public boolean isFull()
    {
    	for (E element : mMembers.list) {
            if (element.getItemPath() == null)
                return false;
    	}
    	return true;
    }

	public E getMember(int memberId) throws ObjectNotFoundException {
        for (E element : mMembers.list) {
            if (element.getID() == memberId)
                return element;
        }
        throw new ObjectNotFoundException("Member "+memberId+" not found in "+mName, "");
    }

	public CollectionMemberList<E> getMembers()
    {
        return mMembers;
    }

	public abstract E addMember(ItemPath itemPath, CastorHashMap props, String classProps) throws MembershipException;

	public abstract E addMember(ItemPath itemPath) throws MembershipException;
	
	public abstract void removeMember(int memberId) throws MembershipException;
	
	@Override
    public boolean equals(Object other) {
		if (!(other instanceof Collection<?>)) return false;
		Collection<?> otherColl = (Collection<?>)other;
    	boolean same = mName.equals(otherColl.getName()) && size() == otherColl.size();
    	if (!same) return false;
		for (Iterator<E> i = getMembers().list.iterator(); i.hasNext();) {
			try {
				E thisMem = i.next();
				CollectionMember otherMem = otherColl.getMember(thisMem.getID());
				if (!thisMem.equals(otherMem)) return false;
			} catch (ObjectNotFoundException ex) {
				return false;
			}
		}
		return true;
    }
}