All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.river.lookup.util.ConsistentMap Maven / Gradle / Ivy

The newest version!
/*
 * 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 org.apache.river.lookup.util;

import java.io.IOException;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.river.api.io.AtomicSerial;
import org.apache.river.api.io.AtomicSerial.GetArg;

/**
 * An implementation of the java.util.Map interface that has
 * a serialized form consistent in all virtual machines. ConsistentMap
 * instances are unmodifiable. All mutator methods, such as add and
 * remove, throw UnsupportedOperationException.
 * This class permits null values and the null key.
 *
 * 

* Although instances of this class are unmodifiable, they are not necessarily * immutable. If a client retrieves a mutable object (either a key or value) contained in a * ConsistentMap and mutates that object, the client in effect * mutates the state of the ConsistentMap. In this case, the * serialized form of the ConsistentMap will most likely also * have been mutated. A ConsistentMap that contains only immutable * objects will maintain a consistent serialized form indefinitely. But a * ConsistentMap that contains mutable objects will maintain a * consistent serialized form only so long as the mutable objects are not * mutated. * * @author Bill Venners */ @AtomicSerial public class ConsistentMap extends AbstractMap implements Serializable { private static final long serialVersionUID = -5223157327307155247L; /** * @serial A Set of java.util.Map.Entry objects, * the key-value pairs contained in this ConsistentMap. */ private final Set> entrySet; /** * Constructs a new, empty ConsistentMap. All instances * of ConsistentMap are unmodifiable. */ public ConsistentMap() { entrySet = new ConsistentSet>(new HashSet>()); } /** * Constructs a new ConsistentMap containing the elements * in the passed collection. All instances of ConsistentMap * are unmodifiable. * * @param init the map whose elements are to be placed into this map. * @throws NullPointerException if the passed init reference * is null */ public ConsistentMap(Map init) { this(entrySet(init)); } public ConsistentMap(GetArg arg) throws IOException { this(entrySet((Map) arg.get("entrySet", null))); } private static Set> entrySet(Map init){ if (init == null) { throw new NullPointerException(); } // Must put the key-value pairs into ConsistentMapEntry objects, // so they'll behave correctly when setValue() is invoked on them. Set> unmodEntries = new HashSet>(init.size()); Set> entries = init.entrySet(); Iterator> it = entries.iterator(); while (it.hasNext()) { Map.Entry entry = it.next(); Map.Entry unmodEntry = new ConsistentMapEntry(entry.getKey(), entry.getValue()); unmodEntries.add(unmodEntry); } return new ConsistentSet>(unmodEntries); } private ConsistentMap(Set> set){ entrySet = set; } /** * Returns a set view of the mappings contained in this * ConsistentMap. Each element in the returned * set is a Map.Entry * * @return a set view of the mappings contained in this * ConsistentMap. */ @Override public Set> entrySet() { return Collections.unmodifiableSet(entrySet); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy