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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.solr.common;
20
import java.io.Serializable;
21
import java.util.ArrayList;
22
import java.util.Collection;
23
import java.util.Iterator;
26
* @version $Id: SolrInputField.java 600589 2007-12-03 16:35:59Z ryan $
29
public class SolrInputField implements Iterable<Object>, Serializable
35
public SolrInputField( String n )
40
//---------------------------------------------------------------
41
//---------------------------------------------------------------
44
* Set the value for a field. Arrays will be converted to a collection.
46
public void setValue(Object v, float b) {
49
if( v instanceof Object[] ) {
50
Object[] arr = (Object[])v;
51
Collection<Object> c = new ArrayList<Object>( arr.length );
52
for( Object o : arr ) {
63
* Add values to a field. if the added value is a collection, each value
64
* will be added individually
66
@SuppressWarnings("unchecked")
67
public void addValue(Object v, float b) {
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.
79
Collection<Object> vals = null;
80
if( value instanceof Collection ) {
81
vals = (Collection<Object>)value;
84
vals = new ArrayList<Object>( 3 );
89
// Add the new values to a collection
90
if( v instanceof Iterable ) {
91
for( Object o : (Iterable<Object>)v ) {
95
else if( v instanceof Object[] ) {
96
for( Object o : (Object[])v ) {
105
//---------------------------------------------------------------
106
//---------------------------------------------------------------
108
@SuppressWarnings("unchecked")
109
public Object getFirstValue() {
110
if( value instanceof Collection ) {
111
Collection c = (Collection<Object>)value;
113
return c.iterator().next();
121
* @return the value for this field. If the field has multiple values, this
122
* will be a collection.
124
public Object getValue() {
129
* @return the values for this field. This will return a collection even
130
* if the field is not multi-valued
132
@SuppressWarnings("unchecked")
133
public Collection<Object> getValues() {
134
if( value instanceof Collection ) {
135
return (Collection<Object>)value;
137
if( value != null ) {
138
Collection<Object> vals = new ArrayList<Object>(1);
146
* @return the number of values for this field
148
public int getValueCount() {
149
if( value instanceof Collection ) {
150
return ((Collection)value).size();
152
return (value == null) ? 0 : 1;
155
//---------------------------------------------------------------
156
//---------------------------------------------------------------
158
public float getBoost() {
162
public void setBoost(float boost) {
166
public String getName() {
170
public void setName(String name) {
174
@SuppressWarnings("unchecked")
175
public Iterator<Object> iterator() {
176
if( value instanceof Collection ) {
177
return ((Collection)value).iterator();
179
return new Iterator<Object>() {
180
boolean nxt = (value!=null);
182
public boolean hasNext() {
186
public Object next() {
191
public void remove() {
192
throw new UnsupportedOperationException();
198
public String toString()
200
return name + "("+boost+")={" + value + "}";