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

it.tidalwave.geo.impl.DefaultGeoFinder Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * forceTen - open source geography
 * Copyright (C) 2007-2012 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://forceten.tidalwave.it
 * SCM: https://bitbucket.org/tidalwave/forceten-src
 *
 **********************************************************************************************************************/
package it.tidalwave.geo.impl;

import it.tidalwave.geo.*;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import it.tidalwave.util.Finder;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.spi.FinderSupport;
import org.openide.util.lookup.Lookups;
import org.openide.util.Parameters;
import it.tidalwave.geo.finder.spi.GeoFinderProvider;

/***********************************************************************************************************************
 *
 * @author Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class DefaultGeoFinder extends FinderSupport> implements GeoFinder
  {
    @Nonnull
    private Object owner;

    @CheckForNull
    private Class type;

    private List withObjects = new ArrayList();

    public DefaultGeoFinder (final @Nonnull Object owner)
      {
        super("GeoFinder for " + owner);
        this.owner = owner;
      }

    @Override
    public DefaultGeoFinder clone()
      {
        final DefaultGeoFinder clone = (DefaultGeoFinder)super.clone();
        clone.owner = this.owner;
        clone.type = this.type;
        clone.withObjects = new ArrayList(this.withObjects);

        return clone;
      }

    @Override @Nonnull
    public  GeoFinder ofType (final @Nonnull Class type)
      {
        final DefaultGeoFinder clone = clone();
        clone.type = type;
        return (GeoFinder)clone;
      }

    @Nonnull
    public GeoFinder with (final @Nonnull Object object)
      {
        final DefaultGeoFinder clone = clone();
        clone.withObjects.add(object);
        return clone;
      }

    @Override @Nonnull
    protected List computeResults()
      {
        try
          {
            Parameters.notNull("ofType() must be called", type);
            final Finder finder = GeoFinderProvider.findFinder(owner, type, Lookups.fixed(withObjects.toArray()));
            final List list = (List)finder.from(firstResult).max(maxResults).results();
            list.addAll(0, Collections.nCopies(firstResult, (T)null));
            return list;
          }
        catch (NotFoundException e)
          {
            return Collections.emptyList();
          }     
      }
  }