Merge "Bug 2361: Added prototype implementation using new SPI."
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / Identifiables.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 javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.concepts.Identifiable;
13
14 import com.google.common.base.Function;
15
16 public final class Identifiables {
17     private static final Function<Identifiable<Object>, Object> EXTRACT_IDENTIFIER = new Function<Identifiable<Object>, Object>() {
18         @Override
19         public Object apply(final @Nonnull Identifiable<Object> input) {
20             Preconditions.checkNotNull(input);
21             return input.getIdentifier();
22         }
23     };
24
25     private Identifiables() {
26         throw new UnsupportedOperationException("Utility class");
27     }
28
29     /**
30      * Return the {@link Function} to extract the identifier from a particular
31      * object implementing the {@link Identifiable} contract.
32      *
33      * @return Identificator associated with the object
34      */
35     /*
36      * Suppressing warnings here allows us to fool the compiler enough
37      * such that we can reuse a single function for all applicable types
38      * and present it in a type-safe manner to our users.
39      */
40     @SuppressWarnings({ "unchecked", "rawtypes" })
41     public static <V> Function<Identifiable<V>, V> identifierExtractor() {
42         return (Function) EXTRACT_IDENTIFIER;
43     }
44 }
45