2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.concepts.util;
10 import java.math.BigDecimal;
11 import java.math.BigInteger;
12 import java.util.Collections;
13 import java.util.HashSet;
15 import java.util.concurrent.atomic.AtomicInteger;
16 import java.util.concurrent.atomic.AtomicLong;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.concepts.Mutable;
21 public class Immutables {
23 private Immutables() {
24 throw new UnsupportedOperationException("Helper class");
27 public static final Set<Class<?>> KNOWN_IMMUTABLES = Immutables.<Class<?>> asHashSet(
29 Integer.class, Short.class, BigDecimal.class, BigInteger.class, Byte.class, Character.class, Double.class,
33 * Determines if object is known to be immutable
35 * Note: This method may return false to immutable objects which
36 * immutability is not known, was defined not using concepts term.
40 * @return true if object is known to be immutable false otherwise.
42 public static boolean isImmutable(Object o) {
44 throw new IllegalArgumentException("Object should not be null");
46 if (o instanceof Mutable) {
48 } else if (o instanceof Immutable) {
50 } else if (o instanceof String) {
52 } else if (KNOWN_IMMUTABLES.contains(o.getClass())) {
58 private static boolean isNumberImmutable(Number o) {
59 if(o instanceof AtomicInteger) {
61 } else if(o instanceof AtomicLong) {
63 } else if(o instanceof Short) {
70 private static <E> Set<E> asHashSet(E... list) {
71 HashSet<E> ret = new HashSet<>();
75 return Collections.unmodifiableSet(ret);