FieldStore.java

73 lines | 2.078 kB Blame History Raw Download
/*
 * Copyright 2010-2011 Ning, Inc.
 *
 * Ning licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.ning.billing.account.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FieldStore implements IFieldStore {
    private final Map<String, ICustomField> fields = new HashMap<String, ICustomField>();

    @Override
    public void setValue(String fieldName, String fieldValue) {
        if (fields.containsKey(fieldName)) {
            fields.get(fieldName).setValue(fieldValue);
        } else {
            fields.put(fieldName, new CustomField(fieldName, fieldValue));
        }
    }

    @Override
    public String getValue(String fieldName) {
        if (fields.containsKey(fieldName)) {
            return fields.get(fieldName).getValue();
        } else {
            return null;
        }
    }

    @Override
    public List<ICustomField> getNewFields() {
        List<ICustomField> newFields = new ArrayList<ICustomField>();
        for (ICustomField field : fields.values()) {
            if (field.isNew()) {
                newFields.add(field);
            }
        }

        return newFields;
    }

    @Override
    public List<ICustomField> getUpdatedFields() {
        List<ICustomField> updatedFields = new ArrayList<ICustomField>();
        for (ICustomField field : fields.values()) {
            if (!field.isNew()) {
                updatedFields.add(field);
            }
        }

        return updatedFields;
    }

    @Override
    public void clear() {
        this.fields.clear();
    }
}