pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / util / fst / PositiveIntOutputs.java
1 package org.apache.lucene.util.fst;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21
22 import org.apache.lucene.store.DataInput;
23 import org.apache.lucene.store.DataOutput;
24
25 /**
26  * Output is a long, for each input term.  NOTE: the
27  * resulting FST is not guaranteed to be minimal!  See
28  * {@link Builder}.  You cannot store 0 output with this
29  * (that's reserved to mean "no output")!
30  *
31  * @lucene.experimental
32  */
33
34 public final class PositiveIntOutputs extends Outputs<Long> {
35   
36   private final static Long NO_OUTPUT = new Long(0);
37
38   private final boolean doShare;
39
40   private final static PositiveIntOutputs singletonShare = new PositiveIntOutputs(true);
41   private final static PositiveIntOutputs singletonNoShare = new PositiveIntOutputs(false);
42
43   private PositiveIntOutputs(boolean doShare) {
44     this.doShare = doShare;
45   }
46
47   public static PositiveIntOutputs getSingleton(boolean doShare) {
48     return doShare ? singletonShare : singletonNoShare;
49   }
50
51   public Long get(long v) {
52     if (v == 0) {
53       return NO_OUTPUT;
54     } else {
55       return Long.valueOf(v);
56     }
57   }
58
59   @Override
60   public Long common(Long output1, Long output2) {
61     assert valid(output1);
62     assert valid(output2);
63     if (output1 == NO_OUTPUT || output2 == NO_OUTPUT) {
64       return NO_OUTPUT;
65     } else if (doShare) {
66       assert output1 > 0;
67       assert output2 > 0;
68       return Math.min(output1, output2);
69     } else if (output1.equals(output2)) {
70       return output1;
71     } else {
72       return NO_OUTPUT;
73     }
74   }
75
76   @Override
77   public Long subtract(Long output, Long inc) {
78     assert valid(output);
79     assert valid(inc);
80     assert output >= inc;
81
82     if (inc == NO_OUTPUT) {
83       return output;
84     } else if (output.equals(inc)) {
85       return NO_OUTPUT;
86     } else {
87       return output - inc;
88     }
89   }
90
91   @Override
92   public Long add(Long prefix, Long output) {
93     assert valid(prefix);
94     assert valid(output);
95     if (prefix == NO_OUTPUT) {
96       return output;
97     } else if (output == NO_OUTPUT) {
98       return prefix;
99     } else {
100       return prefix + output;
101     }
102   }
103
104   @Override
105   public void write(Long output, DataOutput out) throws IOException {
106     assert valid(output);
107     out.writeVLong(output);
108   }
109
110   @Override
111   public Long read(DataInput in) throws IOException {
112     long v = in.readVLong();
113     if (v == 0) {
114       return NO_OUTPUT;
115     } else {
116       return v;
117     }
118   }
119
120   private boolean valid(Long o) {
121     assert o != null;
122     assert o instanceof Long;
123     assert o == NO_OUTPUT || o > 0;
124     return true;
125   }
126
127   @Override
128   public Long getNoOutput() {
129     return NO_OUTPUT;
130   }
131
132   @Override
133   public String outputToString(Long output) {
134     return output.toString();
135   }
136 }