01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *
19: */
20: package org.safehaus.asyncweb.common;
21:
22: import java.io.Serializable;
23: import java.util.Comparator;
24:
25: public class CookieComparator implements Serializable,
26: Comparator<Cookie> {
27:
28: private static final long serialVersionUID = -222644341851192813L;
29:
30: public static final CookieComparator INSTANCE = new CookieComparator();
31:
32: public int compare(Cookie o1, Cookie o2) {
33: int answer;
34:
35: // Compare the name first.
36: answer = o1.getName().compareTo(o2.getName());
37: if (answer != 0) {
38: return answer;
39: }
40:
41: // and then path
42: if (o1.getPath() == null) {
43: if (o2.getPath() != null) {
44: answer = -1;
45: } else {
46: answer = 0;
47: }
48: } else {
49: answer = o1.getPath().compareTo(o2.getPath());
50: }
51:
52: if (answer != 0) {
53: return answer;
54: }
55:
56: // and then domain
57: if (o1.getDomain() == null) {
58: if (o2.getDomain() != null) {
59: answer = -1;
60: } else {
61: answer = 0;
62: }
63: } else {
64: answer = o1.getDomain().compareTo(o2.getDomain());
65: }
66:
67: return answer;
68: }
69:
70: private Object readResolve() {
71: return INSTANCE;
72: }
73: }
|