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 * 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")!
31 * @lucene.experimental
34 public final class PositiveIntOutputs extends Outputs<Long> {
36 private final static Long NO_OUTPUT = new Long(0);
38 private final boolean doShare;
40 private final static PositiveIntOutputs singletonShare = new PositiveIntOutputs(true);
41 private final static PositiveIntOutputs singletonNoShare = new PositiveIntOutputs(false);
43 private PositiveIntOutputs(boolean doShare) {
44 this.doShare = doShare;
47 public static PositiveIntOutputs getSingleton(boolean doShare) {
48 return doShare ? singletonShare : singletonNoShare;
51 public Long get(long v) {
55 return Long.valueOf(v);
60 public Long common(Long output1, Long output2) {
61 assert valid(output1);
62 assert valid(output2);
63 if (output1 == NO_OUTPUT || output2 == NO_OUTPUT) {
68 return Math.min(output1, output2);
69 } else if (output1.equals(output2)) {
77 public Long subtract(Long output, Long inc) {
82 if (inc == NO_OUTPUT) {
84 } else if (output.equals(inc)) {
92 public Long add(Long prefix, Long output) {
95 if (prefix == NO_OUTPUT) {
97 } else if (output == NO_OUTPUT) {
100 return prefix + output;
105 public void write(Long output, DataOutput out) throws IOException {
106 assert valid(output);
107 out.writeVLong(output);
111 public Long read(DataInput in) throws IOException {
112 long v = in.readVLong();
120 private boolean valid(Long o) {
122 assert o instanceof Long;
123 assert o == NO_OUTPUT || o > 0;
128 public Long getNoOutput() {
133 public String outputToString(Long output) {
134 return output.toString();