View Javadoc

1   /*
2    * This file is part of Pease Plate Template Engine.
3    * 
4    * Pease Plate Template Engine is free software: you can redistribute
5    * it and/or modify it under the terms of the GNU Lesser General 
6    * Public License as published by the Free Software Foundation, 
7    * either version 3 of the License, or any later version.
8    * 
9    * Pease Plate Template Engine is distributed in the hope that it 
10   * will be useful, but WITHOUT ANY WARRANTY; without even the implied
11   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12   * See the GNU Lesser General Public License for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public 
15   * License along with Pease Plate Template Engine. If not, see 
16   * <http://www.gnu.org/licenses/>.
17   * 
18   * Copyright (c) 2008 Manfred HANTSCHEL
19   */
20  package org.peaseplate.internal.model;
21  
22  import java.util.Locale;
23  
24  import org.peaseplate.internal.ResourceKey;
25  
26  /**
27   * The default implementation of the {@link ResourceKey} interface.
28   * 
29   * @author Manfred HANTSCHEL
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  	 * @see org.peaseplate.internal.ResourceKey#getName()
52  	 */
53  	public String getName() {
54      	return name;
55      }
56  
57  	/**
58  	 * @see org.peaseplate.internal.ResourceKey#getLocale()
59  	 */
60  	public Locale getLocale() {
61      	return locale;
62      }
63  	
64  	/**
65  	 * @see org.peaseplate.internal.ResourceKey#getEncoding()
66  	 */
67  	public String getEncoding() {
68  	    return encoding;
69      }
70  
71  	/**
72  	 * @see org.peaseplate.internal.ResourceKey#getParent()
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 	 * @see java.lang.Object#equals(java.lang.Object)
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 	 * @see java.lang.Object#hashCode()
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 	 * @see java.lang.Object#toString()
138 	 */
139 	@Override
140     public String toString() {
141 		return getName() + "[" + getLocale() + ", " + getEncoding() + "]";
142     }
143 	
144 }