aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/FeatureList.java
blob: 6f0a69316834e99d627b4c37149089cbce9414df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression;

import com.yahoo.api.annotations.Beta;
import com.yahoo.searchlib.rankingexpression.parser.ParseException;
import com.yahoo.searchlib.rankingexpression.parser.RankingExpressionParser;
import com.yahoo.searchlib.rankingexpression.parser.TokenMgrException;
import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Encapsulates the production rule 'featureList()' in the RankingExpressionParser.
 *
 * @author Simon Thoresen Hult
 */
@Beta
public class FeatureList implements Iterable<ReferenceNode> {

    private final List<ReferenceNode> features = new ArrayList<>();

    /**
     * Creates a new feature list by consuming from a reader object.
     *
     * @param reader The reader that contains the string to parse.
     * @throws ParseException Thrown if the string could not be parsed.
     */
    public FeatureList(Reader reader) throws ParseException {
        features.addAll(parse(reader));
    }

    /**
     * Creates a new feature list by parsing a string.
     *
     * @param list The string to parse.
     * @throws ParseException Thrown if the string could not be parsed.
     */
    public FeatureList(String list) throws ParseException {
        features.addAll(parse(new StringReader(list)));
    }

    /**
     * Creates a new feature list by reading the content of a file.
     *
     * @param file The file whose content to parse.
     * @throws ParseException        Thrown if the string could not be parsed.
     * @throws FileNotFoundException Thrown if the file specified could not be found.
     */
    public FeatureList(File file) throws ParseException, FileNotFoundException {
        features.addAll(parse(new FileReader(file)));
    }

    /**
     * Parses the content of a reader object as a list of feature nodes.
     *
     * @param reader A reader object that contains an feature list.
     * @return A list of those features named in the string.
     * @throws ParseException if the string could not be parsed.
     */
    private static List<ReferenceNode> parse(Reader reader) throws ParseException {
        List<ReferenceNode> lst;
        try {
            lst = new RankingExpressionParser(reader).featureList();
        }
        catch (TokenMgrException e) {
            ParseException t = new ParseException();
            throw (ParseException)t.initCause(e);
        }
        return lst;
    }

    /**
     * Returns the number of features in this list.
     *
     * @return The size.
     */
    public int size() {
        return features.size();
    }

    /**
     * Returns the feature at the given index.
     *
     * @param i the index of the feature to return.
     * @return the feature at the given index.
     */
    public ReferenceNode get(int i) {
        return features.get(i);
    }

    @Override
    public int hashCode() {
        int ret = 0;
        for (ReferenceNode node : features) {
            ret += node.hashCode() * 17;
        }
        return ret;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof FeatureList)) {
            return false;
        }
        FeatureList lst = (FeatureList)obj;
        if (features.size() != lst.features.size()) {
            return false;
        }
        for (int i = 0; i < features.size(); ++i) {
            if (!features.get(i).equals(lst.features.get(i))) {
                return false;
            }
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        for (ReferenceNode node : this) {
            ret.append(node).append(" ");
        }
        return ret.toString();
    }

    @Override
    public Iterator<ReferenceNode> iterator() {
        return features.iterator();
    }

}