All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.mobile.rss.EntityWithProperties Maven / Gradle / Ivy

There is a newer version: 1.0.15
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * Licensed 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: EntityWithProperties.java,v c94a06e77b11 2010/07/04 11:51:33 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.rss;

import it.tidalwave.util.NotFoundException;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import it.tidalwave.util.Id;
import it.tidalwave.util.Key;
import it.tidalwave.util.TypeSafeMultiMap;
import it.tidalwave.util.TypeSafeHashMultiMap;
import it.tidalwave.mobile.media.Media;
import it.tidalwave.util.As;
import it.tidalwave.util.TypeSafeHashMap;
import it.tidalwave.util.TypeSafeMap;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
@Immutable
public class EntityWithProperties implements As, TypeSafeMap
  {    
    private final TypeSafeMap map;

    @Nonnull
    private final Id id;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public EntityWithProperties (final @Nonnull Id id)
      {
        this(id, Collections., Object>emptyMap());
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected EntityWithProperties (final @Nonnull Id id, final @Nonnull Map, Object> map)
      {
        this.id = id;
        this.map = new TypeSafeHashMap(map);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Id getId()
      {
        return id;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public  T get (final @Nonnull Key key)
      throws NotFoundException
      {
        return map.get(key);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public boolean containsKey (final @Nonnull Key item)
      {
        return map.containsKey(item);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Set> getKeys()
      {
        return map.getKeys();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnegative
    public int getSize()
      {
        return map.getSize();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Iterator iterator()
      {
        return map.iterator();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public  E with (final @Nonnull Key key, final @Nonnull T value)
      {
        final Map, Object> asMap = map.asMap();
        final Object oldValue = asMap.get(key);

        if (oldValue != null)
          {
            final Collection list = new ArrayList();
            list.add((T)oldValue);
            list.add(value);
            asMap.put(key, list);
          }
        else
          {
            asMap.put(key, value);
          }
        
        return clone(asMap);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected E clone (final @Nonnull Map, Object> asMap)
      {
        return (E)new EntityWithProperties(id, asMap);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Map, Object> asMap()
      {
        throw new UnsupportedOperationException("Not supported yet.");
      }

    public  T as(Class type)
      {
        throw new UnsupportedOperationException("Not supported yet.");
      }

    public  T as(Class type, NotFoundBehaviour nfb)
      {
        throw new UnsupportedOperationException("Not supported yet.");
      }
  }