add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / util / fst / PairOutputs.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  * Pairs up two outputs into one.
27  *
28  * @lucene.experimental
29  */
30
31 public class PairOutputs<A,B> extends Outputs<PairOutputs.Pair<A,B>> {
32
33   private final Pair<A,B> NO_OUTPUT;
34   private final Outputs<A> outputs1;
35   private final Outputs<B> outputs2;
36
37   public static class Pair<A,B> {
38     public final A output1;
39     public final B output2;
40
41     public Pair(A output1, B output2) {
42       this.output1 = output1;
43       this.output2 = output2;
44     }
45
46     @Override @SuppressWarnings("rawtypes")
47     public boolean equals(Object other) {
48       if (other == this) {
49         return true;
50       } else if (other instanceof Pair) {
51         Pair pair = (Pair) other;
52         return output1.equals(pair.output1) && output2.equals(pair.output2);
53       } else {
54         return false;
55       }
56     }
57
58     @Override
59     public int hashCode() {
60       return output1.hashCode() + output2.hashCode();
61     }
62   };
63
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());
68   }
69   
70   public Pair<A,B> get(A output1, B output2) {
71     if (output1 == outputs1.getNoOutput() && output2 == outputs2.getNoOutput()) {
72       return NO_OUTPUT;
73     } else {
74       return new Pair<A,B>(output1, output2);
75     }
76   }
77  
78   @Override
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));
82   }
83
84   @Override
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));
88   }
89
90   @Override
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));
94   }
95
96   @Override
97   public void write(Pair<A,B> output, DataOutput writer) throws IOException {
98     outputs1.write(output.output1, writer);
99     outputs2.write(output.output2, writer);
100   }
101
102   @Override
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);
107   }
108
109   @Override
110   public Pair<A,B> getNoOutput() {
111     return NO_OUTPUT;
112   }
113
114   @Override
115   public String outputToString(Pair<A,B> output) {
116     return "<pair:" + outputs1.outputToString(output.output1) + "," + outputs2.outputToString(output.output2) + ">";
117   }
118 }