001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.data;
020:
021: import java.io.IOException;
022:
023: import org.restlet.util.Engine;
024:
025: /**
026: * Multi-usage parameter.
027: *
028: * @author Jerome Louvel (contact@noelios.com)
029: */
030: public class Parameter implements Comparable<Parameter> {
031: /** The name. */
032: private String name;
033:
034: /** The value. */
035: private String value;
036:
037: /**
038: * Default constructor.
039: */
040: public Parameter() {
041: this (null, null);
042: }
043:
044: /**
045: * Preferred constructor.
046: *
047: * @param name
048: * The name.
049: * @param value
050: * The value.
051: */
052: public Parameter(String name, String value) {
053: this .name = name;
054: this .value = value;
055: }
056:
057: /**
058: * Compares this object with the specified object for order.
059: *
060: * @param o
061: * The object to be compared.
062: * @return A negative integer, zero, or a positive integer as this object is
063: * less than, equal to, or greater than the specified object.
064: */
065: public int compareTo(Parameter o) {
066: return getName().compareTo(o.getName());
067: }
068:
069: /** {@inheritDoc} */
070: @Override
071: public boolean equals(Object obj) {
072: boolean result = (obj == this );
073:
074: // if obj == this no need to go further
075: if (!result) {
076: // if obj isn't a parameter or is null don't evaluate further
077: if ((obj instanceof Parameter) && obj != null) {
078: Parameter that = (Parameter) obj;
079:
080: if (!(this .name == null)) // compare names taking care of
081: // nulls
082: {
083: result = (this .name.equals(that.name));
084: } else {
085: result = (that.name == null);
086: }
087:
088: if (result) // if names are equal test the values
089: {
090: if (!(this .value == null)) // compare values taking care of
091: // nulls
092: {
093: result = (this .value.equals(that.value));
094: } else {
095: result = (that.value == null);
096: }
097: }
098: }
099: }
100:
101: return result;
102: }
103:
104: /**
105: * Returns the name of this parameter.
106: *
107: * @return The name of this parameter.
108: */
109: public String getName() {
110: return this .name;
111: }
112:
113: /**
114: * Returns the value.
115: *
116: * @return The value.
117: */
118: public String getValue() {
119: return this .value;
120: }
121:
122: /** {@inheritDoc} */
123: @Override
124: public int hashCode() {
125: return Engine.hashCode(getName(), getValue());
126: }
127:
128: /**
129: * Sets the name.
130: *
131: * @param name
132: * The name.
133: */
134: public void setName(String name) {
135: this .name = name;
136: }
137:
138: /**
139: * Sets the value.
140: *
141: * @param value
142: * The value.
143: */
144: public void setValue(String value) {
145: this .value = value;
146: }
147:
148: /**
149: * Returns a string with the name and value of the parameter.
150: *
151: * @return A string with the name and value of the parameter.
152: */
153: @Override
154: public String toString() {
155: return getName() + ": " + getValue();
156: }
157:
158: /**
159: * Encodes the parameter using the standard URI encoding mechanism.
160: *
161: * @param characterSet
162: * The supported character encoding.
163: * @return The encoded string.
164: * @throws IOException
165: */
166: public String encode(CharacterSet characterSet) throws IOException {
167: StringBuilder sb = new StringBuilder();
168: encode(sb, characterSet);
169: return sb.toString();
170: }
171:
172: /**
173: * Encodes the parameter and appends the result to the given buffer. Uses
174: * the standard URI encoding mechanism.
175: *
176: * @param buffer
177: * The buffer to append.
178: * @param characterSet
179: * The supported character encoding
180: * @throws IOException
181: */
182: public void encode(Appendable buffer, CharacterSet characterSet)
183: throws IOException {
184: if (getName() != null) {
185: buffer.append(Reference.encode(getName(), characterSet));
186:
187: if (getValue() != null) {
188: buffer.append('=');
189: buffer.append(Reference
190: .encode(getValue(), characterSet));
191: }
192: }
193: }
194: }
|