1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.peaseplate.internal.model;
21
22 import java.util.Locale;
23
24 import org.peaseplate.internal.ResourceKey;
25
26
27
28
29
30
31 public class DefaultResourceKey implements ResourceKey {
32
33 private static final long serialVersionUID = 1L;
34
35 private final String name;
36 private final Locale locale;
37 private final String encoding;
38
39 public DefaultResourceKey(String name, Locale locale, String encoding) throws IllegalArgumentException {
40 super();
41
42 if (name == null)
43 throw new IllegalArgumentException("Name must not be null");
44
45 this.name = name;
46 this.locale = locale;
47 this.encoding = encoding;
48 }
49
50
51
52
53 public String getName() {
54 return name;
55 }
56
57
58
59
60 public Locale getLocale() {
61 return locale;
62 }
63
64
65
66
67 public String getEncoding() {
68 return encoding;
69 }
70
71
72
73
74 public ResourceKey getParent() {
75 ResourceKey result = null;
76
77 if (locale != null) {
78 if ((locale.getLanguage() != null) && (locale.getLanguage().length() > 0)) {
79 if ((locale.getCountry() != null) && (locale.getCountry().length() > 0)) {
80 if ((locale.getVariant() != null) && (locale.getVariant().length() > 0)) {
81 result = new DefaultResourceKey(
82 getName(), new Locale(locale.getLanguage(), locale.getCountry()), getEncoding()
83 );
84 }
85 else
86 result = new DefaultResourceKey(
87 getName(), new Locale(locale.getLanguage()), getEncoding()
88 );
89 }
90 else
91 result = new DefaultResourceKey(
92 getName(), null, getEncoding()
93 );
94 }
95 }
96
97 return result;
98 }
99
100
101
102
103 @Override
104 public boolean equals(Object object) {
105 if (object == this)
106 return true;
107
108 if (object == null)
109 return false;
110
111 if (!(object instanceof ResourceKey))
112 return false;
113
114 ResourceKey key = (ResourceKey)object;
115
116 if (!getName().equals(key.getName()))
117 return false;
118
119 return (
120 ((getLocale() != null) && (getLocale().equals(key.getLocale()))) ||
121 ((getLocale() == null) && (key.getLocale() == null))
122 );
123 }
124
125
126
127
128 @Override
129 public int hashCode() {
130 return
131 ((getName() != null) ? getName().hashCode() : -1) ^
132 ((getLocale() != null) ? getLocale().hashCode() : -1)
133 ;
134 }
135
136
137
138
139 @Override
140 public String toString() {
141 return getName() + "[" + getLocale() + ", " + getEncoding() + "]";
142 }
143
144 }