001// Copyright 2006 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
018import org.apache.tapestry5.services.PersistentFieldBundle;
019import org.apache.tapestry5.services.PersistentFieldChange;
020
021import java.util.Collection;
022import java.util.Map;
023
024public class PersistentFieldBundleImpl implements PersistentFieldBundle
025{
026    /**
027     * Keyed on componentId + fieldName.
028     */
029    private final Map<String, Object> values = CollectionFactory.newMap();
030
031    public PersistentFieldBundleImpl(Collection<PersistentFieldChange> changes)
032    {
033        for (PersistentFieldChange change : changes)
034        {
035            String key = buildKey(change.getComponentId(), change.getFieldName());
036
037            values.put(key, change.getValue());
038        }
039    }
040
041    private String buildKey(String componentId, String fieldName)
042    {
043        StringBuilder builder = new StringBuilder();
044        if (componentId != null) builder.append(componentId);
045        builder.append(':');
046        builder.append(fieldName);
047
048        return builder.toString();
049    }
050
051    public boolean containsValue(String componentId, String fieldName)
052    {
053        String key = buildKey(componentId, fieldName);
054
055        return values.containsKey(key);
056    }
057
058    public Object getValue(String componentId, String fieldName)
059    {
060        String key = buildKey(componentId, fieldName);
061
062        return values.get(key);
063    }
064
065}