Cleanup use of Guava library
[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 static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.collect.ImmutableSet;
13 import java.math.BigDecimal;
14 import java.math.BigInteger;
15 import java.util.Set;
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 = ImmutableSet.of(
26             Integer.class, Short.class, BigDecimal.class, BigInteger.class, Byte.class, Character.class, Double.class,
27             Float.class, String.class, Boolean.class, Void.class);
28
29     /**
30      * Determines if object is known to be immutable
31      *
32      * <p>Note: This method may return false to immutable objects which
33      * immutability is not known, was defined not using concepts term.
34      *
35      * @param obj
36      *            Reference to check
37      * @return true if object is known to be immutable false otherwise.
38      */
39     public static boolean isImmutable(final Object obj) {
40         checkArgument(obj != null,"Object should not be null");
41         if (obj instanceof Mutable) {
42             return false;
43         } else if (obj instanceof Immutable) {
44             return true;
45         } else if (obj instanceof String) {
46             return true;
47         } else if (KNOWN_IMMUTABLES.contains(obj.getClass())) {
48             return true;
49         }
50         return false;
51     }
52 }