Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.util.List;
Abstraction for a resource representation variant.
 
 public class Variant {
     
     private Locale language;
     private MediaType mediaType;
     private String encoding;
    
    
Create a new instance of Variant

Parameters:
mediaType the media type of the variant - may be null
language the language of the variant - may be null
encoding the content encoding of the variant - may be null
Throws:
java.lang.IllegalArgumentException if all three parameters are null
 
     public Variant(MediaType mediaTypeLocale languageString encoding) {
         if (mediaType==null && language==null && encoding==null)
             throw new IllegalArgumentException("mediaType, language, encoding all null");
         this. = encoding;
         this. = language;
         this. = mediaType;
     }
    
    
Get the language of the variant

Returns:
the language or null if none set
 
     public Locale getLanguage() {
         return ;
     }

    
Get the media type of the variant

Returns:
the media type or null if none set
 
     public MediaType getMediaType() {
         return ;
     }

    
Get the encoding of the variant

Returns:
the encoding or null if none set
 
     public String getEncoding() {
         return ;
     }
    
    
Create a Variant.VariantListBuilder initialized with a set of supported media types.

Parameters:
mediaTypes the available mediaTypes. If specific charsets are supported they should be included as parameters of the respective media type.
Returns:
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 Variant.VariantListBuilder initialized with a set of supported languages.

Parameters:
languages the available languages.
Returns:
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 Variant.VariantListBuilder initialized with a set of supported encodings.

Parameters:
encodings the available encodings.
Returns:
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.

Returns:
the hash code
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + (this. != null ? this..hashCode() : 0);
        hash = 29 * hash + (this. != null ? this..hashCode() : 0);
        hash = 29 * hash + (this. != null ? this..hashCode() : 0);
        return hash;
    }

    
Compares obj to this variant to see if they are the same considering all property values.

Parameters:
obj the object to compare to
Returns:
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 = (Variantobj;
        if (this. != other.language && (this. == null || !this..equals(other.language))) {
            return false;
        }
        if (this. != other.mediaType && (this. == null || !this..equals(other.mediaType))) {
            return false;
        }
        if (this. != other.encoding && (this. == null || !this..equals(other.encoding))) {
            return false;
        }
        return true;
    }
    @Override
    public String toString() {
        StringWriter w = new StringWriter();
        w.append("Variant[mediaType=");
        w.append(==null ? "null" : .toString());
        w.append(", language=");
        w.append(==null ? "null" : .toString());
        w.append(", encoding=");
        w.append(==null ? "null" : );
        w.append("]");
        return w.toString();
    }
    
    
A builder for a list of representation variants.
    public static abstract class VariantListBuilder {
        
        
Protected constructor, use the static newInstance method to obtain an instance.
        protected VariantListBuilder() {}
        
        
Create a new builder instance.

Returns:
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.

Returns:
a list of representation variants
        public abstract List<Variantbuild();
        
        
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 list would have four members:

List<Variant> list = VariantListBuilder.newInstance().languages("en","fr")
   .encodings("zip", "identity").add().build()

Returns:
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.

Parameters:
languages the available languages
Returns:
the updated builder
        public abstract VariantListBuilder languages(Locale... languages);
        
        
Set the encoding[s] for this variant.

Parameters:
encodings the available encodings
Returns:
the updated builder
        public abstract VariantListBuilder encodings(String... encodings);
        
        
Set the media type[s] for this variant.

Parameters:
mediaTypes the available mediaTypes. If specific charsets are supported they should be included as parameters of the respective media type.
Returns:
the updated builder
        public abstract VariantListBuilder mediaTypes(MediaType... mediaTypes);
    }
New to GrepCode? Check out our FAQ X