org.jboss.resteasy.specimpl.VariantListBuilderImpl Maven / Gradle / Ivy
The newest version!
package org.jboss.resteasy.specimpl;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Variant;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* @author Bill Burke
* @version $Revision: 1 $
*/
public class VariantListBuilderImpl extends Variant.VariantListBuilder
{
private ArrayList variants = new ArrayList();
private ArrayList currentLanguages = new ArrayList();
private ArrayList currentEncodings = new ArrayList();
private ArrayList currentTypes = new ArrayList();
public List build()
{
add();
ArrayList copy = new ArrayList();
copy.addAll(variants);
variants.clear();
currentLanguages.clear();
currentEncodings.clear();
currentTypes.clear();
return copy;
}
public Variant.VariantListBuilder add()
{
int langSize = currentLanguages.size();
int encodingSize = currentEncodings.size();
int typeSize = currentTypes.size();
int i = 0;
if (langSize == 0 && encodingSize == 0 && typeSize == 0) return this;
do
{
MediaType type = null;
if (i < typeSize) type = currentTypes.get(i);
int j = 0;
do
{
String encoding = null;
if (j < encodingSize) encoding = currentEncodings.get(j);
int k = 0;
do
{
Locale language = null;
if (k < langSize) language = currentLanguages.get(k);
variants.add(new Variant(type, language, encoding));
k++;
} while (k < langSize);
j++;
} while (j < encodingSize);
i++;
} while (i < typeSize);
currentLanguages.clear();
currentEncodings.clear();
currentTypes.clear();
return this;
}
public Variant.VariantListBuilder languages(Locale... languages)
{
for (Locale language : languages) currentLanguages.add(language);
return this;
}
@Override
public Variant.VariantListBuilder encodings(String... encodings)
{
for (String encoding : encodings) currentEncodings.add(encoding);
return this;
}
@Override
public Variant.VariantListBuilder mediaTypes(MediaType... mediaTypes)
{
for (MediaType type : mediaTypes) currentTypes.add(type);
return this;
}
}