/*
  ==============================================================================

   This file is part of the JUCE library - "Jules' Utility Class Extensions"
   Copyright 2004-10 by Raw Material Software Ltd.

  ------------------------------------------------------------------------------

   JUCE can be redistributed and/or modified under the terms of the GNU General
   Public License (Version 2), as published by the Free Software Foundation.
   A copy of the license is included in the JUCE distribution, or can be found
   online at www.gnu.org/licenses.

   JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  ------------------------------------------------------------------------------

   To release a closed-source product which uses JUCE, commercial licenses are
   available: visit www.rawmaterialsoftware.com/juce for more information.

  ==============================================================================
*/

#include "../core/juce_StandardHeader.h"

BEGIN_JUCE_NAMESPACE

#include "juce_NamedValueSet.h"


//==============================================================================
NamedValueSet::NamedValue::NamedValue() throw()
{
}

inline NamedValueSet::NamedValue::NamedValue (const var::identifier& name_, const var& value_)
    : name (name_), value (value_)
{
}

//==============================================================================
NamedValueSet::NamedValueSet() throw()
{
}

NamedValueSet::NamedValueSet (const NamedValueSet& other)
    : values (other.values)
{
}

NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
{
    values = other.values;
    return *this;
}

NamedValueSet::~NamedValueSet()
{
}

int NamedValueSet::size() const throw()
{
    return values.size();
}

const var& NamedValueSet::operator[] (const var::identifier& name) const
{
    for (int i = values.size(); --i >= 0;)
    {
        const NamedValue& v = values.getReference(i);

        if (v.name == name)
            return v.value;
    }

    return var::null;
}

const var NamedValueSet::getWithDefault (const var::identifier& name, const var& defaultReturnValue) const
{
    const var* v = getItem (name);
    return v != 0 ? *v : defaultReturnValue;
}

var* NamedValueSet::getItem (const var::identifier& name) const
{
    for (int i = values.size(); --i >= 0;)
    {
        NamedValue& v = values.getReference(i);

        if (v.name == name)
            return &(v.value);
    }

    return 0;
}

bool NamedValueSet::set (const var::identifier& name, const var& newValue)
{
    for (int i = values.size(); --i >= 0;)
    {
        NamedValue& v = values.getReference(i);

        if (v.name == name)
        {
            if (v.value == newValue)
                return false;

            v.value = newValue;
            return true;
        }
    }

    values.add (NamedValue (name, newValue));
    return true;
}

bool NamedValueSet::contains (const var::identifier& name) const
{
    return getItem (name) != 0;
}

bool NamedValueSet::remove (const var::identifier& name)
{
    for (int i = values.size(); --i >= 0;)
    {
        if (values.getReference(i).name == name)
        {
            values.remove (i);
            return true;
        }
    }

    return false;
}

const var::identifier NamedValueSet::getName (int index) const
{
    jassert (((unsigned int) index) < (unsigned int) values.size());
    return values [index].name;
}

void NamedValueSet::clear()
{
    values.clear();
}


END_JUCE_NAMESPACE
