~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/solrj/src/java/org/apache/solr/common/SolrInputField.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.solr.common;
19
 
 
20
 
import java.io.Serializable;
21
 
import java.util.ArrayList;
22
 
import java.util.Collection;
23
 
import java.util.Iterator;
24
 
 
25
 
/**
26
 
 * @version $Id: SolrInputField.java 600589 2007-12-03 16:35:59Z ryan $
27
 
 * @since solr 1.3
28
 
 */
29
 
public class SolrInputField implements Iterable<Object>, Serializable
30
 
{
31
 
  String name;
32
 
  Object value = null; 
33
 
  float boost = 1.0f;
34
 
  
35
 
  public SolrInputField( String n )
36
 
  {
37
 
    this.name = n;
38
 
  }
39
 
 
40
 
  //---------------------------------------------------------------
41
 
  //---------------------------------------------------------------
42
 
 
43
 
  /**
44
 
   * Set the value for a field.  Arrays will be converted to a collection.
45
 
   */
46
 
  public void setValue(Object v, float b) {
47
 
    boost = b;
48
 
 
49
 
    if( v instanceof Object[] ) {
50
 
      Object[] arr = (Object[])v;
51
 
      Collection<Object> c = new ArrayList<Object>( arr.length );
52
 
      for( Object o : arr ) {
53
 
        c.add( o );
54
 
      }
55
 
      value = c;
56
 
    }
57
 
    else {
58
 
      value = v;
59
 
    }
60
 
  }
61
 
 
62
 
  /**
63
 
   * Add values to a field.  if the added value is a collection, each value
64
 
   * will be added individually
65
 
   */
66
 
  @SuppressWarnings("unchecked")
67
 
  public void addValue(Object v, float b) {
68
 
    if( value == null ) {
69
 
      setValue(v, b);
70
 
      return;
71
 
    }
72
 
    
73
 
    // The lucene API and solr XML field specification make it possible to set boosts
74
 
    // on multi-value fields even though lucene indexing does not support this.
75
 
    // To keep behavior consistent with what happens in the lucene index, we accumulate
76
 
    // the product of all boosts specified for this field.
77
 
    boost *= b;
78
 
    
79
 
    Collection<Object> vals = null;
80
 
    if( value instanceof Collection ) {
81
 
      vals = (Collection<Object>)value;
82
 
    }
83
 
    else {
84
 
      vals = new ArrayList<Object>( 3 );
85
 
      vals.add( value );
86
 
      value = vals;
87
 
    }
88
 
    
89
 
    // Add the new values to a collection
90
 
    if( v instanceof Iterable ) {
91
 
      for( Object o : (Iterable<Object>)v ) {
92
 
        vals.add( o );
93
 
      }
94
 
    }
95
 
    else if( v instanceof Object[] ) {
96
 
      for( Object o : (Object[])v ) {
97
 
        vals.add( o );
98
 
      }
99
 
    }
100
 
    else {
101
 
      vals.add( v );
102
 
    }
103
 
  }
104
 
 
105
 
  //---------------------------------------------------------------
106
 
  //---------------------------------------------------------------
107
 
  
108
 
  @SuppressWarnings("unchecked")
109
 
  public Object getFirstValue() {
110
 
    if( value instanceof Collection ) {
111
 
      Collection c = (Collection<Object>)value;
112
 
      if( c.size() > 0 ) {
113
 
        return c.iterator().next();
114
 
      }
115
 
      return null;
116
 
    }
117
 
    return value;
118
 
  }
119
 
 
120
 
  /**
121
 
   * @return the value for this field.  If the field has multiple values, this
122
 
   * will be a collection.
123
 
   */
124
 
  public Object getValue() {
125
 
    return value;
126
 
  }
127
 
 
128
 
  /**
129
 
   * @return the values for this field.  This will return a collection even
130
 
   * if the field is not multi-valued
131
 
   */
132
 
  @SuppressWarnings("unchecked")
133
 
  public Collection<Object> getValues() {
134
 
    if( value instanceof Collection ) {
135
 
      return (Collection<Object>)value;
136
 
    }
137
 
    if( value != null ) {
138
 
      Collection<Object> vals = new ArrayList<Object>(1);
139
 
      vals.add( value );
140
 
      return vals;
141
 
    }
142
 
    return null;
143
 
  }
144
 
 
145
 
  /**
146
 
   * @return the number of values for this field
147
 
   */
148
 
  public int getValueCount() {
149
 
    if( value instanceof Collection ) {
150
 
      return ((Collection)value).size();
151
 
    }
152
 
    return (value == null) ? 0 : 1;
153
 
  }
154
 
  
155
 
  //---------------------------------------------------------------
156
 
  //---------------------------------------------------------------
157
 
  
158
 
  public float getBoost() {
159
 
    return boost;
160
 
  }
161
 
 
162
 
  public void setBoost(float boost) {
163
 
    this.boost = boost;
164
 
  }
165
 
 
166
 
  public String getName() {
167
 
    return name;
168
 
  }
169
 
 
170
 
  public void setName(String name) {
171
 
    this.name = name;
172
 
  }
173
 
 
174
 
  @SuppressWarnings("unchecked")
175
 
  public Iterator<Object> iterator() {
176
 
    if( value instanceof Collection ) {
177
 
      return ((Collection)value).iterator();
178
 
    }
179
 
    return new Iterator<Object>() {
180
 
      boolean nxt = (value!=null);
181
 
      
182
 
      public boolean hasNext() {
183
 
        return nxt;
184
 
      }
185
 
 
186
 
      public Object next() {
187
 
        nxt = false;
188
 
        return value;
189
 
      }
190
 
 
191
 
      public void remove() {
192
 
        throw new UnsupportedOperationException();
193
 
      }
194
 
    };
195
 
  }
196
 
 
197
 
  @Override
198
 
  public String toString()
199
 
  {
200
 
    return name + "("+boost+")={" + value + "}";
201
 
  }
202
 
}