1 /*
2 * Copyright 2012 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.handler.codec.http.multipart;
17
18 import java.io.Serializable;
19 import java.util.Comparator;
20
21 final class CaseIgnoringComparator implements Comparator<CharSequence>, Serializable {
22
23 private static final long serialVersionUID = 4582133183775373862L;
24
25 static final CaseIgnoringComparator INSTANCE = new CaseIgnoringComparator();
26
27 private CaseIgnoringComparator() {
28 }
29
30 @Override
31 public int compare(CharSequence o1, CharSequence o2) {
32 int o1Length = o1.length();
33 int o2Length = o2.length();
34 int min = Math.min(o1Length, o2Length);
35 for (int i = 0; i < min; i++) {
36 char c1 = o1.charAt(i);
37 char c2 = o2.charAt(i);
38 if (c1 != c2) {
39 c1 = Character.toUpperCase(c1);
40 c2 = Character.toUpperCase(c2);
41 if (c1 != c2) {
42 c1 = Character.toLowerCase(c1);
43 c2 = Character.toLowerCase(c2);
44 if (c1 != c2) {
45 return c1 - c2;
46 }
47 }
48 }
49 }
50 return o1Length - o2Length;
51 }
52
53 private Object readResolve() {
54 return INSTANCE;
55 }
56 }