
com.taobao.metamorphosis.server.utils.CopyOnWriteMap Maven / Gradle / Ivy
/*
* (C) 2007-2012 Alibaba Group Holding Limited.
*
* 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.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
package com.taobao.metamorphosis.server.utils;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* ??????mina??ĿApache MINA Project
*
* @modification by ??
* @since 2011-8-11 ????3:08:09
*/
public class CopyOnWriteMap implements Map, Cloneable, Serializable {
private static final long serialVersionUID = 788933834504546710L;
private volatile Map internalMap;
public CopyOnWriteMap() {
this.internalMap = new HashMap();
}
@Override
public synchronized int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (this.internalMap == null ? 0 : this.internalMap.hashCode());
return result;
}
@Override
public synchronized boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
CopyOnWriteMap other = (CopyOnWriteMap) obj;
if (this.internalMap == null) {
if (other.internalMap != null) {
return false;
}
}
else if (!this.internalMap.equals(other.internalMap)) {
return false;
}
return true;
}
public CopyOnWriteMap(final int initialCapacity) {
this.internalMap = new HashMap(initialCapacity);
}
public CopyOnWriteMap(final Map data) {
this.internalMap = new HashMap(data);
}
@Override
public V put(final K key, final V value) {
synchronized (this) {
final Map newMap = new HashMap(this.internalMap);
final V val = newMap.put(key, value);
this.internalMap = newMap;
return val;
}
}
@Override
public V remove(final Object key) {
synchronized (this) {
final Map newMap = new HashMap(this.internalMap);
final V val = newMap.remove(key);
this.internalMap = newMap;
return val;
}
}
@Override
public void putAll(final Map extends K, ? extends V> newData) {
synchronized (this) {
final Map newMap = new HashMap(this.internalMap);
newMap.putAll(newData);
this.internalMap = newMap;
}
}
@Override
public void clear() {
synchronized (this) {
this.internalMap = new HashMap();
}
}
@Override
public int size() {
return this.internalMap.size();
}
@Override
public boolean isEmpty() {
return this.internalMap.isEmpty();
}
@Override
public boolean containsKey(final Object key) {
return this.internalMap.containsKey(key);
}
@Override
public boolean containsValue(final Object value) {
return this.internalMap.containsValue(value);
}
@Override
public V get(final Object key) {
return this.internalMap.get(key);
}
@Override
public Set keySet() {
return this.internalMap.keySet();
}
@Override
public Collection values() {
return this.internalMap.values();
}
@Override
public Set> entrySet() {
return this.internalMap.entrySet();
}
@Override
public synchronized String toString() {
return "CopyOnWriteMap [internalMap=" + this.internalMap + "]";
}
@Override
public Object clone() {
try {
return super.clone();
}
catch (final CloneNotSupportedException e) {
throw new InternalError();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy