📄 modificationeventimpl.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.facade.internal;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.rapla.components.util.Tools;
import org.rapla.entities.RaplaObject;
import org.rapla.entities.RaplaType;
import org.rapla.facade.ModificationEvent;
/** Encapsulate the changes that are made in the backend-store.*/
public class ModificationEventImpl implements ModificationEvent
{
Set updatedObjects;
Set removedObjects;
boolean isRefresh = false;
Map updatedIndex;
Map removedIndex;
public ModificationEventImpl() {
isRefresh = true;
}
public ModificationEventImpl(Set changedObjects,Set removedObjects) {
isRefresh = false;
this.updatedObjects = Collections.unmodifiableSet(changedObjects);
this.removedObjects = Collections.unmodifiableSet(removedObjects);
updatedIndex = new HashMap();
Iterator it = updatedObjects.iterator();
while (it.hasNext()) {
addToIndex(updatedIndex,(RaplaObject)it.next());
}
makeIndexReadOnly(updatedIndex);
removedIndex = new HashMap();
it = removedObjects.iterator();
while (it.hasNext()) {
addToIndex(removedIndex,(RaplaObject)it.next());
}
makeIndexReadOnly(removedIndex);
}
private void addToIndex(Map index,RaplaObject type) {
Set set = (Set) index.get(type.getRaplaType());
if (set == null) {
set = new HashSet();
index.put(type.getRaplaType(),set);
}
set.add(type);
}
private void makeIndexReadOnly(Map index) {
Iterator it = index.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
Set set = Collections.unmodifiableSet((Set) index.get(key));
index.put(key,set);
}
}
/** All objects in the cache are modified. This is not selective. */
private boolean isRefresh() {
return isRefresh;
}
private Set retainObjects(Set set,Set col) {
HashSet tempSet = new HashSet(col.size());
tempSet.addAll(col);
tempSet.retainAll(set);
if (tempSet.size() >0)
return tempSet;
else
return Tools.EMPTY_SET;
}
/** returns the modified objects from a given set.*/
public Set getChanged(Set col) {
checkNotRefresh();
return retainObjects(updatedObjects,col);
}
/** returns the removed objects from a given set.*/
public Set getRemoved(Set col) {
checkNotRefresh();
return retainObjects(removedObjects,col);
}
/** returns if the objects has changed.*/
public boolean hasChanged(Object object) {
if (isRefresh())
{
return true;
}
checkNotRefresh();
return updatedObjects.contains(object);
}
/** returns if the objects was removed.*/
public boolean isRemoved(Object object) {
if (isRefresh())
{
return true;
}
checkNotRefresh();
return removedObjects.contains(object);
}
/** returns if the objects has changed or was removed.*/
public boolean isModified(Object object) {
if (isRefresh())
{
return true;
}
checkNotRefresh();
return hasChanged(object) || isRemoved(object);
}
/** returns if an objects of the specied type was changed or removed.*/
public boolean isModified(RaplaType raplaType) {
if (isRefresh())
{
return true;
}
checkNotRefresh();
return updatedIndex.get(raplaType)!=null || removedIndex.get(raplaType)!=null;
}
private Set getFromIndex(Map index,RaplaType raplaType) {
Set set = (Set) index.get(raplaType);
if (set != null)
return set;
else
return Tools.EMPTY_SET;
}
/** returns if an objects of the specied type has changed .*/
public Set getChanged(RaplaType raplaType) {
checkNotRefresh();
return getFromIndex(updatedIndex,raplaType);
}
/** returns if an objects of the specied type was removed .*/
public Set getRemoved(RaplaType raplaType) {
checkNotRefresh();
return getFromIndex(removedIndex,raplaType);
}
/** returns all removed objects .*/
public Set getRemoved() {
checkNotRefresh();
return removedObjects;
}
/** returns all changed object .*/
public Set getChanged() {
checkNotRefresh();
return updatedObjects;
}
private void checkNotRefresh() {
if (isRefresh())
throw new IllegalStateException("Refresh is set. All objects could be changed!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -