pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / analysis / tokenattributes / PayloadAttributeImpl.java
1 package org.apache.lucene.analysis.tokenattributes;
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.Serializable;
21
22 import org.apache.lucene.index.Payload;
23 import org.apache.lucene.util.AttributeImpl;
24
25 /**
26  * The payload of a Token. See also {@link Payload}.
27  */
28 public class PayloadAttributeImpl extends AttributeImpl implements PayloadAttribute, Cloneable, Serializable {
29   private Payload payload;  
30   
31   /**
32    * Initialize this attribute with no payload.
33    */
34   public PayloadAttributeImpl() {}
35   
36   /**
37    * Initialize this attribute with the given payload. 
38    */
39   public PayloadAttributeImpl(Payload payload) {
40     this.payload = payload;
41   }
42   
43   /**
44    * Returns this Token's payload.
45    */ 
46   public Payload getPayload() {
47     return this.payload;
48   }
49
50   /** 
51    * Sets this Token's payload.
52    */
53   public void setPayload(Payload payload) {
54     this.payload = payload;
55   }
56   
57   @Override
58   public void clear() {
59     payload = null;
60   }
61
62   @Override
63   public Object clone()  {
64     PayloadAttributeImpl clone = (PayloadAttributeImpl) super.clone();
65     if (payload != null) {
66       clone.payload = (Payload) payload.clone();
67     }
68     return clone;
69   }
70
71   @Override
72   public boolean equals(Object other) {
73     if (other == this) {
74       return true;
75     }
76     
77     if (other instanceof PayloadAttribute) {
78       PayloadAttributeImpl o = (PayloadAttributeImpl) other;
79       if (o.payload == null || payload == null) {
80         return o.payload == null && payload == null;
81       }
82       
83       return o.payload.equals(payload);
84     }
85     
86     return false;
87   }
88
89   @Override
90   public int hashCode() {
91     return (payload == null) ? 0 : payload.hashCode();
92   }
93
94   @Override
95   public void copyTo(AttributeImpl target) {
96     PayloadAttribute t = (PayloadAttribute) target;
97     t.setPayload((payload == null) ? null : (Payload) payload.clone());
98   }  
99
100   
101 }