1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* http://www.opensource.org/licenses/cddl1.php
* See the License for the specific language governing
* permissions and limitations under the License.
*/
/*
* Variant.java
*
* Created on September 27, 2007, 3:12 PM
*
*/
package javax.ws.rs.core;
import java.io.StringWriter;
import java.util.List;
import java.util.Locale;
import javax.ws.rs.ext.RuntimeDelegate;
/**
* Abstraction for a resource representation variant.
*/
public class Variant {
private Locale language;
private MediaType mediaType;
private String encoding;
/**
* Create a new instance of Variant
* @param mediaType the media type of the variant - may be null
* @param language the language of the variant - may be null
* @param encoding the content encoding of the variant - may be null
* @throws java.lang.IllegalArgumentException if all three parameters are
* null
*/
public Variant(MediaType mediaType, Locale language, String encoding) {
if (mediaType==null && language==null && encoding==null)
throw new IllegalArgumentException("mediaType, language, encoding all null");
this.encoding = encoding;
this.language = language;
this.mediaType = mediaType;
}
/**
* Get the language of the variant
* @return the language or null if none set
*/
public Locale getLanguage() {
return language;
}
/**
* Get the media type of the variant
* @return the media type or null if none set
*/
public MediaType getMediaType() {
return mediaType;
}
/**
* Get the encoding of the variant
* @return the encoding or null if none set
*/
public String getEncoding() {
return encoding;
}
/**
* Create a {@link VariantListBuilder} initialized with a set of supported
* media types.
* @param mediaTypes the available mediaTypes. If specific charsets
* are supported they should be included as parameters of the respective
* media type.
* @return the initailized builder
* @throws java.lang.IllegalArgumentException if mediaTypes is null or
* contains no elements.
*/
public static VariantListBuilder mediaTypes(MediaType... mediaTypes) {
VariantListBuilder b = VariantListBuilder.newInstance();
b.mediaTypes(mediaTypes);
return b;
}
/**
* Create a {@link VariantListBuilder} initialized with a set of supported
* languages.
* @param languages the available languages.
* @return the initailized builder
* @throws java.lang.IllegalArgumentException if languages is null or
* contains no elements.
*/
public static VariantListBuilder languages(Locale... languages) {
VariantListBuilder b = VariantListBuilder.newInstance();
b.languages(languages);
return b;
}
/**
* Create a {@link VariantListBuilder} initialized with a set of supported
* encodings.
* @param encodings the available encodings.
* @return the initailized builder
* @throws java.lang.IllegalArgumentException if encodings is null or
* contains no elements.
*/
public static VariantListBuilder encodings(String... encodings) {
VariantListBuilder b = VariantListBuilder.newInstance();
b.encodings(encodings);
return b;
}
/**
* Generate hash code from variant properties.
* @return the hash code
*/
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + (this.language != null ? this.language.hashCode() : 0);
hash = 29 * hash + (this.mediaType != null ? this.mediaType.hashCode() : 0);
hash = 29 * hash + (this.encoding != null ? this.encoding.hashCode() : 0);
return hash;
}
/**
* Compares obj to this variant to see if they are the same
* considering all property values.
* @param obj the object to compare to
* @return true if the two variants are the same, false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Variant other = (Variant) obj;
if (this.language != other.language && (this.language == null || !this.language.equals(other.language))) {
return false;
}
if (this.mediaType != other.mediaType && (this.mediaType == null || !this.mediaType.equals(other.mediaType))) {
return false;
}
if (this.encoding != other.encoding && (this.encoding == null || !this.encoding.equals(other.encoding))) {
return false;
}
return true;
}
@Override
public String toString() {
StringWriter w = new StringWriter();
w.append("Variant[mediaType=");
w.append(mediaType==null ? "null" : mediaType.toString());
w.append(", language=");
w.append(language==null ? "null" : language.toString());
w.append(", encoding=");
w.append(encoding==null ? "null" : encoding);
w.append("]");
return w.toString();
}
/**
* A builder for a list of representation variants.
*/
public static abstract class VariantListBuilder {
/**
* Protected constructor, use the static <code>newInstance</code>
* method to obtain an instance.
*/
protected VariantListBuilder() {}
/**
* Create a new builder instance.
* @return a new Builder
*/
public static VariantListBuilder newInstance() {
VariantListBuilder b = RuntimeDelegate.getInstance().createVariantListBuilder();
return b;
}
/**
* Build a list of representation variants from the current state of
* the builder. After this method is called the builder is reset to
* an empty state.
* @return a list of representation variants
*/
public abstract List<Variant> build();
/**
* Add the current combination of metadata to the list of supported variants,
* after this method is called the current combination of metadata is emptied.
* If more than one value is supplied for one or more of the variant properties
* then a variant will be generated for each possible combination. E.g.
* in the following <code>list</code> would have four members:
* <p><pre>List<Variant> list = VariantListBuilder.newInstance().languages("en","fr")
* .encodings("zip", "identity").add().build()</pre>
*
* @return the updated builder
* @throws java.lang.IllegalStateException if there is not at least one
* mediaType, language or encoding set for the current variant.
*/
public abstract VariantListBuilder add();
/**
* Set the language[s] for this variant.
* @param languages the available languages
* @return the updated builder
*/
public abstract VariantListBuilder languages(Locale... languages);
/**
* Set the encoding[s] for this variant.
* @param encodings the available encodings
* @return the updated builder
*/
public abstract VariantListBuilder encodings(String... encodings);
/**
* Set the media type[s] for this variant.
* @param mediaTypes the available mediaTypes. If specific charsets
* are supported they should be included as parameters of the respective
* media type.
* @return the updated builder
*/
public abstract VariantListBuilder mediaTypes(MediaType... mediaTypes);
}
}
|