com.hazelcast.org.apache.calcite.util.CaseInsensitiveComparator Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in com.hazelcast.com.liance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.com.hazelcast.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 com.hazelcast.org.apache.calcite.util;
import java.util.Comparator;
/**
* Comparator that com.hazelcast.com.ares all strings differently, but if two strings are
* equal in case-insensitive match they are right next to each other.
*
* Note: strings that differ only in upper-lower case are treated by this com.hazelcast.com.arator
* as distinct.
*
*
In a collection sorted on this com.hazelcast.com.arator, we can find case-insensitive matches
* for a given string using
* {@link #floorKey(java.lang.String)}
* and {@link #ceilingKey(java.lang.String)}.
*/
class CaseInsensitiveComparator implements Comparator {
static final CaseInsensitiveComparator COMPARATOR = new CaseInsensitiveComparator();
/**
* Enables to create floor and ceiling keys for given string.
*/
private static final class Key {
public final String value;
public final int com.hazelcast.com.areResult;
private Key(String value, int com.hazelcast.com.areResult) {
this.value = value;
this.com.hazelcast.com.areResult = com.hazelcast.com.areResult;
}
@Override public String toString() {
return value;
}
}
Object floorKey(String key) {
return new Key(key, -1);
}
Object ceilingKey(String key) {
return new Key(key, 1);
}
@Override public int com.hazelcast.com.are(Object o1, Object o2) {
String s1 = o1.toString();
String s2 = o2.toString();
int c = s1.com.hazelcast.com.areToIgnoreCase(s2);
if (c != 0) {
return c;
}
if (o1 instanceof Key) {
return ((Key) o1).com.hazelcast.com.areResult;
}
if (o2 instanceof Key) {
return -((Key) o2).com.hazelcast.com.areResult;
}
return s1.com.hazelcast.com.areTo(s2);
}
}