1 package org.apache.lucene.util;
3 import java.util.Comparator;
4 import java.util.StringTokenizer;
7 * Licensed to the Apache Software Foundation (ASF) under one or more
8 * contributor license agreements. See the NOTICE file distributed with
9 * this work for additional information regarding copyright ownership.
10 * The ASF licenses this file to You under the Apache License, Version 2.0
11 * (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
25 * Methods for manipulating strings.
29 public abstract class StringHelper {
32 * The StringInterner implementation used by Lucene.
33 * This shouldn't be changed to an incompatible implementation after other Lucene APIs have been used.
35 public static StringInterner interner = new SimpleStringInterner(1024,8);
37 /** Return the same string object for all equal strings */
38 public static String intern(String s) {
39 return interner.intern(s);
43 * Compares two byte[] arrays, element by element, and returns the
44 * number of elements common to both arrays.
46 * @param bytes1 The first byte[] to compare
47 * @param bytes2 The second byte[] to compare
48 * @return The number of common elements.
50 public static final int bytesDifference(byte[] bytes1, int len1, byte[] bytes2, int len2) {
51 int len = len1 < len2 ? len1 : len2;
52 for (int i = 0; i < len; i++)
53 if (bytes1[i] != bytes2[i])
58 private StringHelper() {
62 * @return a Comparator over versioned strings such as X.YY.Z
65 public static Comparator<String> getVersionComparator() {
66 return versionComparator;
69 private static Comparator<String> versionComparator = new Comparator<String>() {
70 public int compare(String a, String b) {
71 StringTokenizer aTokens = new StringTokenizer(a, ".");
72 StringTokenizer bTokens = new StringTokenizer(b, ".");
74 while (aTokens.hasMoreTokens()) {
75 int aToken = Integer.parseInt(aTokens.nextToken());
76 if (bTokens.hasMoreTokens()) {
77 int bToken = Integer.parseInt(bTokens.nextToken());
78 if (aToken != bToken) {
79 return aToken < bToken ? -1 : 1;
82 // a has some extra trailing tokens. if these are all zeroes, thats ok.
89 // b has some extra trailing tokens. if these are all zeroes, thats ok.
90 while (bTokens.hasMoreTokens()) {
91 if (Integer.parseInt(bTokens.nextToken()) != 0)