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 * Pairs up two outputs into one.
28 * @lucene.experimental
31 public class PairOutputs<A,B> extends Outputs<PairOutputs.Pair<A,B>> {
33 private final Pair<A,B> NO_OUTPUT;
34 private final Outputs<A> outputs1;
35 private final Outputs<B> outputs2;
37 public static class Pair<A,B> {
38 public final A output1;
39 public final B output2;
41 public Pair(A output1, B output2) {
42 this.output1 = output1;
43 this.output2 = output2;
46 @Override @SuppressWarnings("rawtypes")
47 public boolean equals(Object other) {
50 } else if (other instanceof Pair) {
51 Pair pair = (Pair) other;
52 return output1.equals(pair.output1) && output2.equals(pair.output2);
59 public int hashCode() {
60 return output1.hashCode() + output2.hashCode();
64 public PairOutputs(Outputs<A> outputs1, Outputs<B> outputs2) {
65 this.outputs1 = outputs1;
66 this.outputs2 = outputs2;
67 NO_OUTPUT = new Pair<A,B>(outputs1.getNoOutput(), outputs2.getNoOutput());
70 public Pair<A,B> get(A output1, B output2) {
71 if (output1 == outputs1.getNoOutput() && output2 == outputs2.getNoOutput()) {
74 return new Pair<A,B>(output1, output2);
79 public Pair<A,B> common(Pair<A,B> pair1, Pair<A,B> pair2) {
80 return get(outputs1.common(pair1.output1, pair2.output1),
81 outputs2.common(pair1.output2, pair2.output2));
85 public Pair<A,B> subtract(Pair<A,B> output, Pair<A,B> inc) {
86 return get(outputs1.subtract(output.output1, inc.output1),
87 outputs2.subtract(output.output2, inc.output2));
91 public Pair<A,B> add(Pair<A,B> prefix, Pair<A,B> output) {
92 return get(outputs1.add(prefix.output1, output.output1),
93 outputs2.add(prefix.output2, output.output2));
97 public void write(Pair<A,B> output, DataOutput writer) throws IOException {
98 outputs1.write(output.output1, writer);
99 outputs2.write(output.output2, writer);
103 public Pair<A,B> read(DataInput in) throws IOException {
104 A output1 = outputs1.read(in);
105 B output2 = outputs2.read(in);
106 return get(output1, output2);
110 public Pair<A,B> getNoOutput() {
115 public String outputToString(Pair<A,B> output) {
116 return "<pair:" + outputs1.outputToString(output.output1) + "," + outputs2.outputToString(output.output2) + ">";