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

it.tidalwave.openrdf.elmo.impl.ElmoAs Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueMarine Semantic - open source media workflow
 * Copyright (C) 2008-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://bluemarine.tidalwave.it
 * SCM: https://kenai.com/hg/bluemarine~semantic-src
 *
 **********************************************************************************************************************/
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.tidalwave.openrdf.elmo.impl;

import it.tidalwave.openrdf.elmo.ElmoAsSupport;
import it.tidalwave.util.As;
import it.tidalwave.util.AsException;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;

/**
 *
 * @author fritz
 */
public class ElmoAs implements As
  {
    @Nonnull
    private final Object owner;

    // This is flushed at each transaction boundary
    private Map, Object> asMap = new HashMap, Object>();

    /***************************************************************************
     *
     *
     **************************************************************************/
    public ElmoAs (@Nonnull final Object owner)
      {
        this.owner = owner;
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    @Nonnull
    public  T as (@Nonnull final Class clazz)
      {
        return as(clazz, new NotFoundBehaviour()
          {
            public T run (@Nonnull final Throwable t)
              {
                throw new AsException(clazz, t);
              }
          });
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    @Nonnull
    public  T as (@Nonnull final Class clazz, @Nonnull final NotFoundBehaviour notFoundBehaviour)
      {
        Object as = asMap.get(clazz);

        if (as != null)
          {
            return (T)as;
          }

        as = ElmoAsSupport.as(owner, clazz);
        asMap.put(clazz, as); // FIXME: should you cache the result from notFoundBehaviour?

        return (T)((as != null) ? as : notFoundBehaviour.run(null));
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy