1 package org.apache.lucene.util.fst;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
22 import org.apache.lucene.store.DataInput;
23 import org.apache.lucene.store.DataOutput;
26 * Holds one or two longs for each input term. If it's a
27 * single output, Long is returned; else, TwoLongs. Order
28 * is preseved in the TwoLongs case, ie .first is the first
29 * input/output added to Builder, and .second is the
30 * second. You cannot store 0 output with this (that's
31 * reserved to mean "no output")!
33 * NOTE: the resulting FST is not guaranteed to be minimal!
34 * See {@link Builder}.
36 * @lucene.experimental
39 public final class UpToTwoPositiveIntOutputs extends Outputs<Object> {
41 public final static class TwoLongs {
45 public TwoLongs(long first, long second) {
53 public String toString() {
54 return "TwoLongs:" + first + "," + second;
58 public boolean equals(Object _other) {
59 if (_other instanceof TwoLongs) {
60 final TwoLongs other = (TwoLongs) _other;
61 return first == other.first && second == other.second;
68 public int hashCode() {
69 return (int) ((first^(first>>>32)) ^ (second^(second>>32)));
73 private final static Long NO_OUTPUT = new Long(0);
75 private final boolean doShare;
77 private final static UpToTwoPositiveIntOutputs singletonShare = new UpToTwoPositiveIntOutputs(true);
78 private final static UpToTwoPositiveIntOutputs singletonNoShare = new UpToTwoPositiveIntOutputs(false);
80 private UpToTwoPositiveIntOutputs(boolean doShare) {
81 this.doShare = doShare;
84 public static UpToTwoPositiveIntOutputs getSingleton(boolean doShare) {
85 return doShare ? singletonShare : singletonNoShare;
88 public Long get(long v) {
92 return Long.valueOf(v);
96 public TwoLongs get(long first, long second) {
97 return new TwoLongs(first, second);
101 public Long common(Object _output1, Object _output2) {
102 assert valid(_output1, false);
103 assert valid(_output2, false);
104 final Long output1 = (Long) _output1;
105 final Long output2 = (Long) _output2;
106 if (output1 == NO_OUTPUT || output2 == NO_OUTPUT) {
108 } else if (doShare) {
111 return Math.min(output1, output2);
112 } else if (output1.equals(output2)) {
120 public Long subtract(Object _output, Object _inc) {
121 assert valid(_output, false);
122 assert valid(_inc, false);
123 final Long output = (Long) _output;
124 final Long inc = (Long) _inc;
125 assert output >= inc;
127 if (inc == NO_OUTPUT) {
129 } else if (output.equals(inc)) {
137 public Object add(Object _prefix, Object _output) {
138 assert valid(_prefix, false);
139 assert valid(_output, true);
140 final Long prefix = (Long) _prefix;
141 if (_output instanceof Long) {
142 final Long output = (Long) _output;
143 if (prefix == NO_OUTPUT) {
145 } else if (output == NO_OUTPUT) {
148 return prefix + output;
151 final TwoLongs output = (TwoLongs) _output;
152 final long v = prefix;
153 return new TwoLongs(output.first + v, output.second + v);
158 public void write(Object _output, DataOutput out) throws IOException {
159 assert valid(_output, true);
160 if (_output instanceof Long) {
161 final Long output = (Long) _output;
162 out.writeVLong(output<<1);
164 final TwoLongs output = (TwoLongs) _output;
165 out.writeVLong((output.first<<1) | 1);
166 out.writeVLong(output.second);
171 public Object read(DataInput in) throws IOException {
172 final long code = in.readVLong();
173 if ((code & 1) == 0) {
175 final long v = code >>> 1;
179 return Long.valueOf(v);
183 final long first = code >>> 1;
184 final long second = in.readVLong();
185 return new TwoLongs(first, second);
189 private boolean valid(Long o) {
191 assert o instanceof Long;
192 assert o == NO_OUTPUT || o > 0;
196 // Used only by assert
197 private boolean valid(Object _o, boolean allowDouble) {
199 assert _o instanceof Long;
200 return valid((Long) _o);
201 } else if (_o instanceof TwoLongs) {
204 return valid((Long) _o);
209 public Object getNoOutput() {
214 public String outputToString(Object output) {
215 return output.toString();
219 public Object merge(Object first, Object second) {
220 assert valid(first, false);
221 assert valid(second, false);
222 return new TwoLongs((Long) first, (Long) second);