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