ICU-4360 sort like q values in descending order (stable sort)

X-SVN-Rev: 18123
This commit is contained in:
Steven R. Loomis 2005-07-01 20:29:22 +00:00
parent 6c8437aa84
commit fee1acbab1
2 changed files with 39 additions and 14 deletions

View File

@ -1122,7 +1122,8 @@ public class ULocaleTest extends TestFmwk {
/*1*/ { "en", "true" },
/*2*/ { "en", "false" },
/*3*/ { null, "false" },
/*4*/ { "es", "true" }, };
/*4*/ { "es", "true" },
/*5*/ { "de", "true" }};
private static final String ACCEPT_LANGUAGE_HTTP[] = {
/*0*/ "mt-mt, ja;q=0.76, en-us;q=0.95, en;q=0.92, en-gb;q=0.89, fr;q=0.87, iu-ca;q=0.84, iu;q=0.82, ja-jp;q=0.79, mt;q=0.97, de-de;q=0.74, de;q=0.71, es;q=0.68, it-it;q=0.66, it;q=0.63, vi-vn;q=0.61, vi;q=0.58, nl-nl;q=0.55, nl;q=0.53, th-th-traditional;q=.01",
@ -1139,7 +1140,8 @@ public class ULocaleTest extends TestFmwk {
"xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
"xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
"xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, xxx-yyy;q=.01, "+
"es"};
"es",
/*5*/ "de;q=.9, fr;q=.9, xxx-yyy, sr;q=.8"};
public void TestAcceptLanguage() {

View File

@ -2657,6 +2657,33 @@ public final class ULocale implements Serializable {
public static ULocale acceptLanguage(String acceptLanguageList, ULocale[]
availableLocales, boolean[] fallback) {
/**
* @internal ICU 3.4
* @deprecated this is an internal class
*/
class ULocaleAcceptLanguageQ implements Comparable {
private double q;
private double serial;
public ULocaleAcceptLanguageQ(double theq, int theserial) {
q = theq;
serial = theserial;
}
public int compareTo(Object o) {
ULocaleAcceptLanguageQ other = (ULocaleAcceptLanguageQ) o;
if(q > other.q) { // reverse - to sort in descending order
return -1;
} else if(q < other.q) {
return 1;
}
if(serial < other.serial) {
return -1;
} else if(serial > other.serial) {
return 1;
} else {
return 0; // same object
}
}
}
// 1st: parse out the acceptLanguageList into an array
@ -2703,7 +2730,9 @@ public final class ULocale implements Serializable {
}
String loc = acceptLanguageList.substring(n,paramEnd).trim();
map.put(new Double(1.0-q), new ULocale(canonicalize(loc))); // sort in reverse order.. 1.0, 0.9, 0.8 .. etc
int serial = map.size();
ULocaleAcceptLanguageQ entry = new ULocaleAcceptLanguageQ(q,serial);
map.put(entry, new ULocale(canonicalize(loc))); // sort in reverse order.. 1.0, 0.9, 0.8 .. etc
n = itemEnd; // get next item. (n++ will skip over delimiter)
}
@ -2714,7 +2743,6 @@ public final class ULocale implements Serializable {
return acceptLanguage(acceptList, availableLocales, fallback);
}
/**
* @draft ICU 3.4
* @deprecated This is a draft API and might change in a future release of ICU.
@ -2732,7 +2760,6 @@ public final class ULocale implements Serializable {
if(fallback != null) {
fallback[0]=true;
}
// System.out.println("exact match: " + acceptLanguageList[i]);
return acceptLanguageList[i];
}
@ -2742,27 +2769,23 @@ public final class ULocale implements Serializable {
if(len>maxLen) {
maxLen = len;
// System.out.println("maxLen now " + maxLen);
}
}
Locale loc = acceptLanguageList[i].toLocale();
String parent = LocaleUtility.fallback(loc).toString();
acceptFallbacks[i] = parent;
// System.out.println("new fallback " + parent);
}
// OK, no direct one.
// OK, no direct match.
for(maxLen--;maxLen>0;maxLen--) {
for(i=0;i<acceptFallbacks.length;i++) {
if(acceptFallbacks[i]!=null) {
if(acceptFallbacks[i].length() == maxLen) {
// System.out.println("Trying " + i + " - " + acceptFallbacks[i]);
for(j=0;j<availableLocales.length;j++) {
if(availableLocales[j].equals(acceptFallbacks[i])) {
if(fallback != null) {
fallback[0]=false;
}
// System.out.println("fallback match: " + acceptLanguageList[i]);
return new ULocale(acceptFallbacks[i]);
}