Java tutorial
//package com.java2s; /* * Copyright (c) 2009 Stephan Schloepke and innoQ Deutschland GmbH * * Stephan Schloepke: http://www.schloepke.de/ * innoQ Deutschland GmbH: http://www.innoq.com/ * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class Main { /** * Creates an unmodifiable shallow copy of the given original {@link Map}. <p> While the copy returns an immutable * copy of the {@link Map} the content is not cloned in any way. Unless the content is immutable by itself the * result is not fully immutable. In the shallow copy all references are the same as in the original {@link Map}! * </p> * * @param original The {@link Map} to copy the elements fro. * @param <K> The type of the key * @param <V> The type of the value * * @return Returns an immutable (unmodifiable) copy of the original {@link Map} with all the elements added but not * cloned! */ public static <K, V> Map<K, V> createUnmodifiableShallowCopy(final Map<K, V> original) { if (original == null || original.isEmpty()) { return Collections.emptyMap(); } else { return Collections.unmodifiableMap(new HashMap<K, V>(original)); } } /** * Creates an unmodifiable shallow copy of the given original {@link Set}. <p> While the copy returns an immutable * copy of the {@link Set} the content is not cloned in any way. Unless the content is immutable by itself the * result is not fully immutable. In the shallow copy all references are the same as in the original {@link Set}! * </p> * * @param original The {@link Set} to copy the elements fro. * @param <E> The type of the elements * * @return Returns an immutable (unmodifiable) copy of the original {@link Set} with all the elements added but not * cloned! */ public static <E> Set<E> createUnmodifiableShallowCopy(final Set<E> original) { if (original == null || original.isEmpty()) { return Collections.emptySet(); } else { return Collections.unmodifiableSet(new HashSet<E>(original)); } } /** * Creates an unmodifiable shallow copy of the given original {@link List}. <p> While the copy returns an immutable * copy of the {@link List} the content is not cloned in any way. Unless the content is immutable by itself the * result is not fully immutable. In the shallow copy all references are the same as in the original {@link List}! * </p> * * @param original The {@link List} to copy the elements fro. * @param <E> The type of the elements * * @return Returns an immutable (unmodifiable) copy of the original {@link List} with all the elements added but not * cloned! */ public static <E> List<E> createUnmodifiableShallowCopy(final List<E> original) { if (original == null || original.isEmpty()) { return Collections.emptyList(); } else { return Collections.unmodifiableList(new ArrayList<E>(original)); } } }