org.codehaus.httpcache4j.preference.Preference Maven / Gradle / Ivy
/*
* Copyright (c) 2008, The Codehaus. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.codehaus.httpcache4j.preference;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.codehaus.httpcache4j.Directive;
import org.codehaus.httpcache4j.Directives;
import org.codehaus.httpcache4j.Header;
import org.codehaus.httpcache4j.MIMEType;
import org.codehaus.httpcache4j.util.NumberUtils;
import java.util.*;
//TODO: Fix this to not have a type constructor, this is pretty useless.
public class Preference {
private final T preference;
private final double quality;
public Preference(T preference) {
this(preference, 1.0);
}
public Preference(T preference, double quality) {
Preconditions.checkNotNull(preference, "Preference may not be null, use a ALL preference instead.");
Preconditions.checkArgument(quality <= 1.0 && quality > 0.0, "Quality is a percentage ranging from 0.0, to 1.0");
this.preference = preference;
this.quality = quality;
}
public T getPreference() {
return preference;
}
public double getQuality() {
return quality;
}
@Override
public String toString() {
StringBuilder headerValue = new StringBuilder();
if (getQuality() != 1.0) {
headerValue.append(getStringValue()).append(";q=").append(getQuality());
}
else {
headerValue.append(getStringValue());
}
return headerValue.toString();
}
protected String getStringValue() {
return getPreference().toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Preference> that = (Preference>) o;
if (preference != null ? !preference.equals(that.preference) : that.preference != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
return preference != null ? preference.hashCode() : 0;
}
public static List> wrap(T... values) {
return Lists.transform(Arrays.asList(values), new Function>() {
@Override
public Preference apply(T input) {
return new Preference(input);
}
});
}
public static Function toStringF() {
return new Function() {
@Override
public String apply(T input) {
return input.toString();
}
};
}
public static Header toHeader(String headerName, List extends Preference> preferences, Function f) {
ArrayList> pref = new ArrayList>(preferences);
Collections.sort(pref, new PreferenceComparator());
StringBuilder builder = new StringBuilder();
for (Preference preference : pref) {
if (builder.length() > 0) {
builder.append(", ");
}
builder.append(f.apply(preference.getPreference()));
if (preference.getQuality() != 1.0) {
builder.append(";q=").append(preference.getQuality());
}
}
return new Header(headerName, builder.toString());
}
public static List> parse(Header header, Function f) {
ArrayList> accept = new ArrayList>();
Directives directives = header.getDirectives();
for (Directive directive : directives) {
String loc = directive.getName();
T value = f.apply(loc);
if (value == null) {
throw new IllegalArgumentException("Transformation turned value to null");
}
double quality = NumberUtils.toDouble(directive.getParameterValue("q"), 1.0);
accept.add(new Preference(value, quality));
}
Collections.sort(accept, new PreferenceComparator());
return Collections.unmodifiableList(accept);
}
public static Function LocaleToString = new Function() {
@Override
public String apply(Locale input) {
String language = input.getLanguage();
if (input.getCountry() != null && !input.getCountry().trim().isEmpty()) {
return language + "-" + input.getCountry().toLowerCase(Locale.ENGLISH);
}
return language;
}
};
public static Function LocaleParse = new Function() {
@Override
public Locale apply(String input) {
String[] parts = input.split("-", 2);
return new Locale(parts[0], parts.length == 2 ? parts[1].toUpperCase() : "");
}
};
public static Function CharsetToString = new Function() {
@Override
public String apply(Charset input) {
return input.toString();
}
};
public static Function CharsetParse = new Function() {
@Override
public Charset apply(String input) {
return new Charset(input);
}
};
public static Function MIMEParse = new Function() {
@Override
public MIMEType apply(String input) {
return MIMEType.valueOf(input);
}
};
public static class PreferenceComparator implements Comparator> {
@Override
public int compare(Preference o1, Preference o2) {
return Double.compare(o2.getQuality(), o1.getQuality());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy