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

« back to all changes in this revision

Viewing changes to lucene/src/java/org/apache/lucene/util/Counter.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.concurrent.atomic.AtomicLong;
4
 
 
5
 
/**
6
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
7
 
 * contributor license agreements.  See the NOTICE file distributed with
8
 
 * this work for additional information regarding copyright ownership.
9
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
10
 
 * (the "License"); you may not use this file except in compliance with
11
 
 * the License.  You may obtain a copy of the License at
12
 
 *
13
 
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 
 *
15
 
 * Unless required by applicable law or agreed to in writing, software
16
 
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 
 * See the License for the specific language governing permissions and
19
 
 * limitations under the License.
20
 
 */
21
 
 
22
 
/**
23
 
 * Simple counter class
24
 
 * 
25
 
 * @lucene.internal
26
 
 * @lucene.experimental
27
 
 */
28
 
public abstract class Counter {
29
 
 
30
 
  /**
31
 
   * Adds the given delta to the counters current value
32
 
   * 
33
 
   * @param delta
34
 
   *          the delta to add
35
 
   * @return the counters updated value
36
 
   */
37
 
  public abstract long addAndGet(long delta);
38
 
 
39
 
  /**
40
 
   * Returns the counters current value
41
 
   * 
42
 
   * @return the counters current value
43
 
   */
44
 
  public abstract long get();
45
 
 
46
 
  /**
47
 
   * Returns a new counter. The returned counter is not thread-safe.
48
 
   */
49
 
  public static Counter newCounter() {
50
 
    return newCounter(false);
51
 
  }
52
 
 
53
 
  /**
54
 
   * Returns a new counter.
55
 
   * 
56
 
   * @param threadSafe
57
 
   *          <code>true</code> if the returned counter can be used by multiple
58
 
   *          threads concurrently.
59
 
   * @return a new counter.
60
 
   */
61
 
  public static Counter newCounter(boolean threadSafe) {
62
 
    return threadSafe ? new AtomicCounter() : new SerialCounter();
63
 
  }
64
 
 
65
 
  private final static class SerialCounter extends Counter {
66
 
    private long count = 0;
67
 
 
68
 
    @Override
69
 
    public long addAndGet(long delta) {
70
 
      return count += delta;
71
 
    }
72
 
 
73
 
    @Override
74
 
    public long get() {
75
 
      return count;
76
 
    };
77
 
  }
78
 
 
79
 
  private final static class AtomicCounter extends Counter {
80
 
    private final AtomicLong count = new AtomicLong();
81
 
 
82
 
    @Override
83
 
    public long addAndGet(long delta) {
84
 
      return count.addAndGet(delta);
85
 
    }
86
 
 
87
 
    @Override
88
 
    public long get() {
89
 
      return count.get();
90
 
    }
91
 
 
92
 
  }
93
 
}