da5521f7425eb9ff9ee4f1f26f0ba967367195c3
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / 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.util;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableSet;
12 import java.math.BigDecimal;
13 import java.math.BigInteger;
14 import java.util.Set;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.concepts.Mutable;
17
18 public final class Immutables {
19
20     private Immutables() {
21         throw new UnsupportedOperationException("Helper class");
22     }
23
24     public static final Set<Class<?>> KNOWN_IMMUTABLES = ImmutableSet.of(
25             Integer.class, Short.class, BigDecimal.class, BigInteger.class, Byte.class, Character.class, Double.class,
26             Float.class, String.class, Boolean.class, Void.class);
27
28     /**
29      * Determines if object is known to be immutable
30      *
31      * <p>Note: This method may return false to immutable objects which
32      * immutability is not known, was defined not using concepts term.
33      *
34      * @param o
35      *            Reference to check
36      * @return true if object is known to be immutable false otherwise.
37      */
38     public static boolean isImmutable(final Object o) {
39         Preconditions.checkArgument(o != null,"Object should not be null");
40         if (o instanceof Mutable) {
41             return false;
42         } else if (o instanceof Immutable) {
43             return true;
44         } else if (o instanceof String) {
45             return true;
46         } else if (KNOWN_IMMUTABLES.contains(o.getClass())) {
47             return true;
48         }
49         return false;
50     }
51 }