org.quartz.utils.DirtyFlagMap Maven / Gradle / Ivy
Go to download
SDK for dev_appserver (local development) with some of the dependencies shaded (repackaged)
/*
* Copyright 2004-2005 OpenSymphony
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
/*
* Previously Copyright (c) 2001-2004 James House
*/
package org.quartz.utils;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
*
* An implementation of Map
that wraps another Map
* and flags itself 'dirty' when it is modified.
*
*
* @author James House
*/
public class DirtyFlagMap implements Map, Cloneable, java.io.Serializable {
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Data members.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
private static final long serialVersionUID = 1433884852607126222L;
private boolean dirty = false;
private transient boolean locked = false;
private Map map;
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constructors.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/**
*
* Create a DirtyFlagMap that 'wraps' the given Map
.
*
*/
public DirtyFlagMap(Map mapToWrap) {
if (mapToWrap == null)
throw new IllegalArgumentException("mapToWrap cannot be null!");
map = mapToWrap;
}
/**
*
* Create a DirtyFlagMap that 'wraps' a HashMap
.
*
*
* @see java.util.HashMap
*/
public DirtyFlagMap() {
map = new HashMap();
}
/**
*
* Create a DirtyFlagMap that 'wraps' a HashMap
that has the
* given initial capacity.
*
*
* @see java.util.HashMap
*/
public DirtyFlagMap(int initialCapacity) {
map = new HashMap(initialCapacity);
}
/**
*
* Create a DirtyFlagMap that 'wraps' a HashMap
that has the
* given initial capacity and load factor.
*
*
* @see java.util.HashMap
*/
public DirtyFlagMap(int initialCapacity, float loadFactor) {
map = new HashMap(initialCapacity, loadFactor);
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Interface.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public void setMutable(boolean mutable) {
this.locked = !mutable;
if(locked)
map = Collections.unmodifiableMap(map);
else
map = new HashMap(map);
}
public boolean isMutable() {
return !locked;
}
/**
*
* Clear the 'dirty' flag (set dirty flag to false
).
*
*/
public void clearDirtyFlag() {
dirty = false;
}
/**
*
* Determine whether the Map
is flagged dirty.
*
*/
public boolean isDirty() {
return dirty;
}
/**
*
* Get a direct handle to the underlying Map.
*
*/
public Map getWrappedMap() {
return map;
}
public void clear() {
dirty = true;
map.clear();
}
public boolean containsKey(Object key) {
return map.containsKey(key);
}
public boolean containsValue(Object val) {
return map.containsValue(val);
}
public Set entrySet() {
return map.entrySet();
}
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof DirtyFlagMap)) return false;
return map.equals(((DirtyFlagMap) obj).getWrappedMap());
}
public Object get(Object key) {
return map.get(key);
}
public boolean isEmpty() {
return map.isEmpty();
}
public Set keySet() {
return map.keySet();
}
public Object put(Object key, Object val) {
dirty = true;
return map.put(key, val);
}
public void putAll(Map t) {
if (!t.isEmpty()) dirty = true;
map.putAll(t);
}
public Object remove(Object key) {
Object obj = map.remove(key);
if (obj != null) dirty = true;
return obj;
}
public int size() {
return map.size();
}
public Collection values() {
return map.values();
}
public Object clone() {
DirtyFlagMap copy;
try {
copy = (DirtyFlagMap) super.clone();
if (map instanceof HashMap)
copy.map = (Map) ((HashMap) map).clone();
} catch (CloneNotSupportedException ex) {
throw new IncompatibleClassChangeError("Not Cloneable.");
}
return copy;
}
}