 |
Gridarta Editor
|
Go to the documentation of this file.
20 package net.sf.gridarta.model.index;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.Serializable;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Iterator;
32 import java.util.Map.Entry;
33 import java.util.concurrent.CopyOnWriteArrayList;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
49 private final Object
sync =
new Object();
55 private final Collection<IndexListener<V>>
indexListeners =
new CopyOnWriteArrayList<>();
69 private final Map<V, String>
names =
new HashMap<>();
76 private final Collection<V>
pending =
new HashSet<>();
105 synchronized (
sync) {
106 final String nameLowerCase =
name.toLowerCase();
107 final Collection<V> result =
new HashSet<>();
108 for (
final Entry<V, String> e :
names.entrySet()) {
109 if (e.getValue().toLowerCase().contains(nameLowerCase)) {
110 result.add(e.getKey());
119 synchronized (
sync) {
128 synchronized (
sync) {
130 throw new IllegalStateException(
"no transaction is active");
136 if (!tmp.isEmpty()) {
138 for (
final V value : tmp) {
143 for (
final V value : tmp) {
145 listener.valueRemoved(value);
146 listener.nameChanged();
154 public void add(@NotNull
final V value,
final long timestamp) {
155 synchronized (
sync) {
156 final Long oldTimestamp =
timestamps.put(value, timestamp);
157 final boolean notifyPending;
158 final boolean notifyAdded;
159 if (oldTimestamp ==
null || oldTimestamp != timestamp) {
161 notifyAdded = oldTimestamp ==
null;
164 notifyPending =
false;
170 listener.valueAdded(value);
175 listener.pendingChanged();
182 public void remove(@NotNull
final V value) {
183 synchronized (
sync) {
191 listener.valueRemoved(value);
192 listener.nameChanged();
199 synchronized (
sync) {
200 final boolean notifyPending =
pending.add(value) &&
pending.size() == 1;
205 listener.valueAdded(value);
210 listener.pendingChanged();
217 public void setName(@NotNull
final V value,
final long timestamp, @NotNull
final String
name) {
218 synchronized (
sync) {
220 final String oldName =
names.put(value,
name);
221 if (oldName !=
null && oldName.equals(
name)) {
226 listener.nameChanged();
233 public String
getName(@NotNull
final V value) {
234 synchronized (
sync) {
235 return names.get(value);
242 synchronized (
sync) {
243 final Iterator<V>
it =
pending.iterator();
248 final V result =
it.next();
256 synchronized (
sync) {
263 synchronized (
sync) {
279 public void save(@NotNull
final ObjectOutputStream objectOutputStream)
throws IOException {
280 synchronized (
sync) {
282 objectOutputStream.writeObject(
names);
291 @SuppressWarnings(
"unchecked")
292 public
void load(@NotNull final ObjectInputStream objectInputStream) throws IOException {
293 synchronized (
sync) {
294 final Map<V, Long> tmpTimestamps;
295 final Map<V, String> tmpNames;
297 tmpTimestamps = (Map<V, Long>) objectInputStream.readObject();
298 tmpNames = (Map<V, String>) objectInputStream.readObject();
299 }
catch (
final ClassNotFoundException ex) {
300 throw new IOException(ex.getMessage(), ex);
303 throw new IOException(
"cannot restore state within active transaction");
307 tmpNames.keySet().retainAll(tmpTimestamps.keySet());
311 names.putAll(tmpNames);
317 synchronized (
sync) {
329 listener.indexingFinished();
boolean modified
Whether the state (timestamps or names) was modified since last save.
void removeIndexListener(@NotNull final IndexListener< V > listener)
Removes an IndexListener to be notified of changes.
void add(@NotNull final V value, final long timestamp)
Adds a value to the cache.
void addIndexListener(@NotNull final IndexListener< V > listener)
Adds an IndexListener to be notified of changes.
boolean transaction
Whether a transaction is active.
final Map< V, String > names
Maps value to name.
void setName(@NotNull final V value, final long timestamp, @NotNull final String name)
Associates a value with a name.
void setPending(@NotNull final V value)
Marks a value as pending.
final Collection< IndexListener< V > > indexListeners
The registered listeners.
void beginUpdate()
Starts an update.
V removePending()
Returns one pending value.
String getName(@NotNull final V value)
Returns the name associated with a value.
void save(@NotNull final ObjectOutputStream objectOutputStream)
Saves the state to an ObjectOutputStream.
final Map< V, Long > timestamps
Maps value to timestamp.
final Collection< V > transactionDelete
The values to delete at the end of the current transaction.
boolean isModified()
Returns whether the state was modified since last save.
Collection< V > findPartialName(@NotNull final String name)
Returns all matching values for a (possibly partial) key name.
Abstract base class for Index implementations.
Interface for listeners interested in Index related events.
void clear()
Clears all values from the index.
final Collection< V > pending
Pending values.
void indexingFinished()
Should be called after indexing has finished.
int size()
Returns the number of values in this cache.
boolean hasPending()
Returns whether at least one pending value exists.
void load(@NotNull final ObjectInputStream objectInputStream)
@noinspection unchecked
void endUpdate()
Ends an update.
final Object sync
Objects used to synchronize accesses to other fields.
This document describes some hints and requirements for general development on the CrossfireEditor If you plan to make changes to the editor code or setup please read the following and keep it in derived from a basic editor application called Gridder by Pasi Ker�nen so please communicate with best through the cf devel mailing before considering any fundamental changes About code DO NOT USE TABS No matter what Java development platform you are please configure insert indent Tabs are displayed totally different in every editor and there are millions of different editors out there The insertion of tabs in the source code is messing up the syntax formatting in a way that is UNREPAIRABLE Apart from please keep code indentation accurate This is not just good it helps to keep code readable and in that way dramatically decreases the chance for overlooked bugs Everyone is welcomed to correct indentation errors wherever they are spotted Before you start to do this please double check that your editor is really configured to insert spaces Line feeds may be checked in either in windows or in unix linux style All reasonable text and java editors can deal with both linefeed formats Converting line feeds is but in this case please make sure that only linefeed characters are changed and nothing else is affected Due to the platform independent nature of the editor has the potential to run on almost any given operating system the build process differs greatly between systems as well as java environments In the several people have attempted to add build scripts along with structural changes to optimize the setup on one particular system environment which has led to conflict Please do *not *attempt to change the structure or any directories for the mere purpose of improving a build process or performance in a java environment Build scripts may be placed in the root it would be especially fine if it is just one or two files but the latter is not required Please excuse me for placing such restriction I and many users of the editor greatly appreciate build scripts We just had some real troubles over this issue in the past and I don t want to have them repeated the editor has relatively high performance requirements I ve spent a lot of extra work to keep everything as fast and memory efficient as possible when you add new data fields or calculations in the archetype please make sure they are as efficient as possible and worth both the time and space they consume Now don t be afraid too much No development would be possible without adding calculations and data at all Just bear in mind unlike for many other open source performance does make a difference for the CrossfireEditor The for as many systems as possible In case you are unexperienced with java and note that the graphics look different on every and with every font They also have different sizes proportions and behave different A seemingly trivial and effectless change can wreck havoc for the same GUI run on another system please don t be totally afraid of it