001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005
006/**
007 * Filter for matching against a metadata field.
008 */
009public class MetadataFieldFilter {
010
011    private String field;
012    private String stringValue;
013    private int intValue;
014
015    /**
016     * Create a filter for matching against a string metadata field.
017     * @param field the field to match against.
018     * @param value the value to match against.
019     */
020    public MetadataFieldFilter(String field, String value) {
021
022        this.field = field;
023        this.stringValue = value;
024    }
025
026    /**
027     * Create a filter for matching against a numeric metadata field.
028     * @param field the field to match against.
029     * @param value the value to match against.
030     */
031    public MetadataFieldFilter(String field, int value) {
032
033        this.field = field;
034        this.intValue = value;
035    }
036
037    /**
038     * Create a filter for matching against a metadata field defined in JSON.
039     * @param jsonObj the JSON object to construct the filter from.
040     */
041    public MetadataFieldFilter(JsonObject jsonObj) {
042        this.field = jsonObj.get("field").asString();
043
044        JsonValue value = jsonObj.get("value");
045        if (value.isString()) {
046            this.stringValue = value.asString();
047        } else {
048            this.intValue = value.asInt();
049        }
050    }
051
052    /**
053     * Get the JSON representation of the metadata field filter.
054     * @return the JSON object representing the filter.
055     */
056    public JsonObject getJsonObject() {
057
058        JsonObject obj = new JsonObject();
059        obj.add("field", this.field);
060
061        if (this.stringValue != null) {
062            obj.add("value", this.stringValue);
063        } else {
064            obj.add("value", this.intValue);
065        }
066
067        return obj;
068    }
069}