9b7ec5694fa872ee3df0a4a92df92c5583bfebbd
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / StringIdentifier.java
1 /*
2  * Copyright (c) 2016 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.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.yangtools.concepts.Identifier;
14
15 /**
16  * Utility {@link Identifier} backed by a {@link String}.
17  */
18 @Beta
19 public final class StringIdentifier implements Identifier, Comparable<StringIdentifier> {
20     private static final long serialVersionUID = 1L;
21     private final String string;
22
23     public StringIdentifier(final String string) {
24         this.string = Preconditions.checkNotNull(string);
25     }
26
27     public String getString() {
28         return string;
29     }
30
31     @Override
32     public int hashCode() {
33         return string.hashCode();
34     }
35
36     @Override
37     public boolean equals(final Object o) {
38         return this == o || (o instanceof StringIdentifier && string.equals(((StringIdentifier)o).string));
39     }
40
41     @Override
42     public String toString() {
43         return MoreObjects.toStringHelper(StringIdentifier.class).add("string", string).toString();
44     }
45
46     @Override
47     public int compareTo(final StringIdentifier o) {
48         return string.compareTo(o.string);
49     }
50 }