Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
blueBill Core - open source birding Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it) 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. WWW: http://bluebill.tidalwave.it SCM: https://java.net/hg/bluebill~core-src /
 
 package it.tidalwave.mobile.media;
 
 import  javax.annotation.CheckForNull;
 import  javax.annotation.Nonnegative;
 import  javax.annotation.Nonnull;
 import  javax.annotation.concurrent.Immutable;
 import java.util.Date;
 import java.util.Map;
 import java.util.Set;
 import java.net.URI;
 import java.net.URL;
 import  org.openide.util.Lookup;
 import  it.tidalwave.util.As;
 import  it.tidalwave.util.AsException;
 import  it.tidalwave.util.Id;
 import  it.tidalwave.util.Key;
 import  it.tidalwave.util.NotFoundException;
 import  it.tidalwave.util.TypeSafeMap;
 import  it.tidalwave.util.TypeSafeHashMap;
 import  it.tidalwave.role.spi.DefaultIdentifiable;
 import  it.tidalwave.role.Identifiable;
 import  it.tidalwave.role.Removable;
 import  it.tidalwave.netbeans.util.AsLookupSupport;
FIXME: refactor over EntityWithProperties!

Author(s):
Fabrizio Giudici
Version:
$Id$ /
 
 @Immutable
 public class Media extends AsLookupSupport implements As, Lookup.Provider
   {
     public static final Class<MediaMedia = Media.class;
     
     private final static long serialVersionUID = 876756656347568673L;
     
     private static final Logger log = LoggerFactory.getLogger(Media.class);
 
     public static final Key<GenericEntityAUTHOR = new Key<GenericEntity>(.);
     public static final Key<GenericEntitySOURCE = new Key<GenericEntity>(.);
     public static final Key<StringTITLE = new Key<String>(.);
     public static final Key<DateDATE = new Key<Date>(.);
     public static final Key<Id> ID = new Key<Id>(.);
     public static final Key<StringRIGHTS = new Key<String>(.);
     public static final Key<StringFORMAT = new Key<String>(.);
     public static final Key<StringTYPE = new Key<String>(.);
 
     // FIXME: remove, use standard vocabularies.
     public static final String XX = "urn:bluebill-mobile:";
     public static final Key<URIRES_URL = new Key<URI>( + "url");
     public static final Key<StringMIME_TYPE = new Key<String>( + "mime-type");
     public static final Key<IntegerDURATION = new Key<Integer>( + "duration");
 
     @Nonnull
     private final TypeSafeMap propertyMap;
 
     @CheckForNull
     private Downloadable downloadable;

    
/
 
     public Media()
       {
        this(Collections.<Key<?>, Object>emptyMap());
      }

    
/
    private Media (final @Nonnull Map<Key<?>, ObjectpropertyMap)
      {
        this. = new TypeSafeHashMap(propertyMap);
      }

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

    
/
    public boolean contains (final @Nonnull Key<?> item)
      {
        return .containsKey(item);
      }

    
/
    @Nonnull
    public Set<Key<?>> getKeys()
      {
        return .getKeys();
      }

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

    
/
    @Nonnull
    public Iterator<Objectiterator()
      {
        return .iterator();
      }

    
/
    @Nonnull
    public <T> Media with (final @Nonnull Key<T> keyfinal @Nonnull T value)
      {
        if (key.equals())
          {
            final String[] parts = ((String)value).split(";");
            final String mimeType = parts[0];
            Media media = with(mimeType);
            if (parts.length > 1)
              {
                final String durationAsString = parts[1].replaceAll("s$""").trim();
                if (!durationAsString.equals(""))
                  {
                    media = media.with(, Integer.valueOf(durationAsString));
                  }
              }
            return media;
          }
        final Map<Key<?>, ObjectpropertyMapAsPlainMap = .asMap();
        propertyMapAsPlainMap.put(keyvalue);
        propertyMapAsPlainMap.remove();
        if (propertyMapAsPlainMap.containsKey())
          {
            propertyMapAsPlainMap.remove();
            if (((String)propertyMapAsPlainMap.get()).startsWith("image/"))
              {
                propertyMapAsPlainMap.put(..stringValue());
              }
            else if(((StringpropertyMapAsPlainMap.get()).startsWith("audio/"))
              {
                propertyMapAsPlainMap.put(..stringValue());
              }
          }
        if (propertyMapAsPlainMap.containsKey() || propertyMapAsPlainMap.containsKey())
          {
            final StringBuilder buffer = new StringBuilder();
            final Key<?>[] keys = new Key<?>[] {  };
            String separator = "";
            for (final Key<?> k : keys)
              {
                buffer.append(separator);
                if (k.equals())
                  {
                    buffer.append(propertyMapAsPlainMap.get(k));
                    separator = "; ";
                  }
                else if (propertyMapAsPlainMap.containsKey(k))
                  {
                    buffer.append(propertyMapAsPlainMap.get(k)).append("s");
                    separator = "; ";
                  }
              }
            if (buffer.length() > 0)
              {
                propertyMapAsPlainMap.put(buffer.toString());
              }
          }
        return new Media(propertyMapAsPlainMap);
      }

    
/
    @Override @Nonnull
    public <T> T as (final @Nonnull Class<T> typefinal @Nonnull NotFoundBehaviour<T> nfb)
      {
        if (type.equals(Identifiable.class))
          {
            try
              {
                return type.cast(new DefaultIdentifiable(get()));
              }
            catch (NotFoundException e)
              {
                throw new AsException(typee);
              }
          }
        
        if (type.equals(Downloadable.class) || type.equals(Removable.class))
          {
            return super.as(type, new NotFoundBehaviour<T>()
              {
                @Nonnull
                public synchronized T run (final Throwable t)
                  {
                    if (downloadable == null)
                      {
                        try
                          {
                            downloadable = new DefaultDownloadable(new URL(Media.this.get(ID).stringValue()));
                          }
                        catch (MalformedURLException e)
                          {
                            return nfb.run(new AsException(type, e));
                          }
                        catch (NotFoundException e)
                          {
                            return nfb.run(new AsException(type, e));
                          }
                      }
                    return type.cast(downloadable);
                  }
              });
          }
        return super.as(typenfb);
      }

    
/
    @Override @Nonnull
    public String toString()
      {
        try
          {
            return String.format("Media@%x[%s, %s]", System.identityHashCode(this), .get(), );
          }
        catch (NotFoundException e)
          {
            return String.format("Media@%x[%s]", System.identityHashCode(this), );
          }
      }
  }
New to GrepCode? Check out our FAQ X