summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java
blob: 90fd28e8d51bce7ca6cc51ac3dfceaa4d2295fa1 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.anjocaido.groupmanager.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.anjocaido.groupmanager.GroupManager;
import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
import org.anjocaido.groupmanager.utils.StringPermissionComparator;

/**
 * 
 * @author gabrielcouto
 */
public abstract class DataUnit {

	private WorldDataHolder dataSource;
	private String uUID;
	private String lastName;
	private boolean changed, sorted = false;
	private List<String> permissions = Collections.unmodifiableList(Collections.<String>emptyList());

	public DataUnit(WorldDataHolder dataSource, String name) {

		this.dataSource = dataSource;
		this.uUID = name;
	}

	public DataUnit(String name) {

		this.uUID = name;
	}

	/**
	 * Every group is matched only by their names and DataSources names.
	 * 
	 * @param o
	 * @return true if they are equal. false if not.
	 */
	@Override
	public boolean equals(Object o) {

		if (o instanceof DataUnit) {
			DataUnit go = (DataUnit) o;
			if (this.getUUID().equalsIgnoreCase(go.getUUID())) {
				// Global Group match.
				if (this.dataSource == null && go.getDataSource() == null)
					return true;
				// This is a global group, the object to test isn't.
				if (this.dataSource == null && go.getDataSource() != null)
					return false;
				// This is not a global group, but the object to test is.
				if (this.dataSource != null && go.getDataSource() == null)
					return false;
				// Match on group name and world name.
				if (this.dataSource.getName().equalsIgnoreCase(go.getDataSource().getName()))
					return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode() {

		int hash = 5;
		hash = 71 * hash + (this.uUID != null ? this.uUID.toLowerCase().hashCode() : 0);
		return hash;
	}

	/**
	 * Set the data source to point to a different worldDataHolder
	 * 
	 * @param source
	 */
	public void setDataSource(WorldDataHolder source) {

		this.dataSource = source;
	}

	/**
	 * Get the current worldDataHolder this object is pointing to
	 * 
	 * @return the dataSource
	 */
	public WorldDataHolder getDataSource() {

		return dataSource;
	}
	
	public String getUUID() {

		return uUID;
	}
	
	public String getLastName() {

		if (uUID.length() < 36)
			return this.uUID;
		
		return this.lastName;
	}
	
	public void setLastName(String lastName) {

		if (!lastName.equals(this.lastName)) {
			
			this.lastName = lastName;
			changed = true;
			
		}
		
	}

	public void flagAsChanged() {

		WorldDataHolder testSource = getDataSource();
		String source = "";

		if (testSource == null)
			source = "GlobalGroups";
		else
			source = testSource.getName();

		GroupManager.logger.finest("DataSource: " + source + " - DataUnit: " + getUUID() + " flagged as changed!");
		// for(StackTraceElement st: Thread.currentThread().getStackTrace()){
		// GroupManager.logger.finest(st.toString());
		// }
		sorted = false;
		changed = true;
	}

	public boolean isChanged() {

		return changed;
	}

	public void flagAsSaved() {

		WorldDataHolder testSource = getDataSource();
		String source = "";

		if (testSource == null)
			source = "GlobalGroups";
		else
			source = testSource.getName();

		GroupManager.logger.finest("DataSource: " + source + " - DataUnit: " + getUUID() + " flagged as saved!");
		changed = false;
	}

	public boolean hasSamePermissionNode(String permission) {

		return permissions.contains(permission);
	}

	public void addPermission(String permission) {

		if (!hasSamePermissionNode(permission)) {
			List<String> clone = new ArrayList<String>(permissions);
			clone.add(permission);
			permissions = Collections.unmodifiableList(clone);
		}
		flagAsChanged();
	}

	public boolean removePermission(String permission) {

		flagAsChanged();
		List<String> clone = new ArrayList<String>(permissions);
		boolean ret = clone.remove(permission);
		permissions = Collections.unmodifiableList(clone);
		return ret;
	}

	/**
	 * Use this only to list permissions.
	 * You can't edit the permissions using the returned ArrayList instance
	 * 
	 * @return a copy of the permission list
	 */
	public List<String> getPermissionList() {
		sortPermissions();
		return permissions;
	}

	public boolean isSorted() {

		return this.sorted;
	}

	public void sortPermissions() {

		if (!isSorted()) {
			List<String> clone = new ArrayList<String>(permissions);
			Collections.sort(clone, StringPermissionComparator.getInstance());
			permissions = Collections.unmodifiableList(clone);
			sorted = true;
		}
	}
}