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;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.concepts.Mutable;
19 public final class Immutables {
21 private Immutables() {
22 throw new UnsupportedOperationException("Helper class");
25 public static final Set<Class<?>> KNOWN_IMMUTABLES = Immutables.<Class<?>> asHashSet(
27 Integer.class, Short.class, BigDecimal.class, BigInteger.class, Byte.class, Character.class, Double.class,
28 Float.class, String.class);
31 * Determines if object is known to be immutable
33 * Note: This method may return false to immutable objects which
34 * immutability is not known, was defined not using concepts term.
38 * @return true if object is known to be immutable false otherwise.
40 public static boolean isImmutable(final Object o) {
42 throw new IllegalArgumentException("Object should not be null");
44 if (o instanceof Mutable) {
46 } else if (o instanceof Immutable) {
48 } else if (o instanceof String) {
50 } else if (KNOWN_IMMUTABLES.contains(o.getClass())) {
57 private static <E> Set<E> asHashSet(final E... list) {
58 HashSet<E> ret = new HashSet<>();
62 return Collections.unmodifiableSet(ret);