~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/java/org/apache/lucene/util/StringHelper.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.util;
2
 
 
3
 
import java.util.Comparator;
4
 
import java.util.StringTokenizer;
5
 
 
6
 
/**
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
13
 
 *
14
 
 *     http://www.apache.org/licenses/LICENSE-2.0
15
 
 *
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.
21
 
 */
22
 
 
23
 
 
24
 
/**
25
 
 * Methods for manipulating strings.
26
 
 *
27
 
 * @lucene.internal
28
 
 */
29
 
public abstract class StringHelper {
30
 
  /**
31
 
   * Expert:
32
 
   * The StringInterner implementation used by Lucene.
33
 
   * This shouldn't be changed to an incompatible implementation after other Lucene APIs have been used.
34
 
   */
35
 
  public static StringInterner interner = new SimpleStringInterner(1024,8);
36
 
 
37
 
  /** Return the same string object for all equal strings */
38
 
  public static String intern(String s) {
39
 
    return interner.intern(s);
40
 
  }
41
 
 
42
 
  /**
43
 
   * Compares two byte[] arrays, element by element, and returns the
44
 
   * number of elements common to both arrays.
45
 
   *
46
 
   * @param bytes1 The first byte[] to compare
47
 
   * @param bytes2 The second byte[] to compare
48
 
   * @return The number of common elements.
49
 
   */
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])
54
 
        return i;
55
 
    return len;
56
 
  }
57
 
  
58
 
  private StringHelper() {
59
 
  }
60
 
  
61
 
  /**
62
 
   * @return a Comparator over versioned strings such as X.YY.Z
63
 
   * @lucene.internal
64
 
   */
65
 
  public static Comparator<String> getVersionComparator() {
66
 
    return versionComparator;
67
 
  }
68
 
  
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, ".");
73
 
      
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;
80
 
          }
81
 
        } else {
82
 
          // a has some extra trailing tokens. if these are all zeroes, thats ok.
83
 
          if (aToken != 0) {
84
 
            return 1; 
85
 
          }
86
 
        }
87
 
      }
88
 
      
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)
92
 
          return -1;
93
 
      }
94
 
      
95
 
      return 0;
96
 
    }
97
 
  };
98
 
}