1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.misc; |
|
27 |
|
28 import com.oracle.solaris.vp.util.misc.ObjectUtil; |
|
29 |
|
30 public class NameValue<V> implements Comparable<NameValue<V>> { |
|
31 // |
|
32 // Instance data |
|
33 // |
|
34 |
|
35 private String name; |
|
36 private V value; |
|
37 |
|
38 // |
|
39 // Constructors |
|
40 // |
|
41 |
|
42 public NameValue(String name, V value) { |
|
43 this.name = name; |
|
44 this.value = value; |
|
45 } |
|
46 |
|
47 // |
|
48 // Comparable methods |
|
49 // |
|
50 |
|
51 /** |
|
52 * Compares this {@code NameValue} to another based on name. If equal, |
|
53 * attempts to compare based on value. |
|
54 */ |
|
55 @Override |
|
56 @SuppressWarnings({"unchecked"}) |
|
57 public int compareTo(NameValue<V> o) { |
|
58 if (o == null) { |
|
59 return 1; |
|
60 } |
|
61 |
|
62 String n1 = getName(); |
|
63 String n2 = o.getName(); |
|
64 int result = ObjectUtil.compare(n1, n2); |
|
65 |
|
66 if (result == 0) { |
|
67 V v1 = getValue(); |
|
68 V v2 = o.getValue(); |
|
69 if (v1 instanceof Comparable && v2 instanceof Comparable) { |
|
70 result = ObjectUtil.compare((Comparable)v1, (Comparable)v2); |
|
71 } |
|
72 } |
|
73 |
|
74 return result; |
|
75 } |
|
76 |
|
77 // |
|
78 // Object methods |
|
79 // |
|
80 |
|
81 @Override |
|
82 public boolean equals(Object o) { |
|
83 if (o != null && o instanceof NameValue) { |
|
84 NameValue nv = (NameValue)o; |
|
85 return ObjectUtil.equals(getName(), nv.getName()) && |
|
86 ObjectUtil.equals(getValue(), nv.getValue()); |
|
87 } |
|
88 |
|
89 return false; |
|
90 } |
|
91 |
|
92 @Override |
|
93 public int hashCode() { |
|
94 int hash = 7; |
|
95 hash = 67 * hash + (name != null ? name.hashCode() : 0); |
|
96 hash = 67 * hash + (value != null ? value.hashCode() : 0); |
|
97 return hash; |
|
98 } |
|
99 |
|
100 |
|
101 @Override |
|
102 public String toString() { |
|
103 return getName(); |
|
104 } |
|
105 |
|
106 // |
|
107 // NameValue methods |
|
108 // |
|
109 |
|
110 public String getName() { |
|
111 return name; |
|
112 } |
|
113 |
|
114 public V getValue() { |
|
115 return value; |
|
116 } |
|
117 } |
|