BUG-1275: Expose XmlStreamUtils.writeValue()
[yangtools.git] / common / concepts / src / main / java / org / opendaylight / yangtools / concepts / util / Immutables.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.concepts.util;
9
10 import java.math.BigDecimal;
11 import java.math.BigInteger;
12 import java.util.Collections;
13 import java.util.HashSet;
14 import java.util.Set;
15
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.concepts.Mutable;
18
19 public final class Immutables {
20
21     private Immutables() {
22         throw new UnsupportedOperationException("Helper class");
23     }
24
25     public static final Set<Class<?>> KNOWN_IMMUTABLES = Immutables.<Class<?>> asHashSet(
26             //
27             Integer.class, Short.class, BigDecimal.class, BigInteger.class, Byte.class, Character.class, Double.class,
28             Float.class, String.class);
29
30     /**
31      * Determines if object is known to be immutable
32      *
33      * Note: This method may return false to immutable objects which
34      * immutability is not known, was defined not using concepts term.
35      *
36      * @param o
37      *            Reference to check
38      * @return true if object is known to be immutable false otherwise.
39      */
40     public static boolean isImmutable(final Object o) {
41         if (o == null) {
42             throw new IllegalArgumentException("Object should not be null");
43         }
44         if (o instanceof Mutable) {
45             return false;
46         } else if (o instanceof Immutable) {
47             return true;
48         } else if (o instanceof String) {
49             return true;
50         } else if (KNOWN_IMMUTABLES.contains(o.getClass())) {
51             return true;
52         }
53         return false;
54     }
55
56     @SafeVarargs
57     private static <E> Set<E> asHashSet(final E... list) {
58         HashSet<E> ret = new HashSet<>();
59         for (E e : list) {
60             ret.add(e);
61         }
62         return Collections.unmodifiableSet(ret);
63     }
64 }