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

core.main.ReferenceMap Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * 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.
 */
package br.com.objectos.rio.core.main;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import br.com.objectos.rio.RioComponent;
import br.com.objectos.rio.core.Sysout;
import br.com.objectos.rio.core.fs.FileSystem;
import br.com.objectos.rio.core.http.Http;
import br.com.objectos.rio.core.tar.Tar;
import br.com.objectos.rio.eclipse.Eclipse;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;

/**
 * @author [email protected] (Marcio Endo)
 */
class ReferenceMap {

  private final Map> workaroundMap = ImmutableMap
      .> builder()
      .put("eclipse", Eclipse.class)
      .put("http", Http.class)
      .put("fs", FileSystem.class)
      .put("sysout", Sysout.class)
      .put("tar", Tar.class)
      .build();
  private final RioMethodMap rioMethodMap = RioMethodMap.mapOf(workaroundMap.values());

  private final Rio rio;
  private final VariableMap variableMap;

  public ReferenceMap(Rio rio, VariableMap variableMap) {
    this.rio = rio;
    this.variableMap = variableMap;
  }

  public Reference fromMethodName(String methodName, List argumentList) {
    Optional maybeMethod = rioMethodMap.get(methodName, argumentList);
    if (!maybeMethod.isPresent()) {
      throw new IllegalArgumentException();
    }

    Method method = maybeMethod.get();
    @SuppressWarnings("unchecked")
    Class declaringClass = (Class) method.getDeclaringClass();
    return getInstance(declaringClass);
  }

  public Reference fromReferenceName(String referenceName) {
    if (workaroundMap.containsKey(referenceName)) {
      Class type = workaroundMap.get(referenceName);
      return getInstance(type);
    }

    Optional maybeVariable = variableMap.get(referenceName);
    if (maybeVariable.isPresent()) {
      Variable variable = maybeVariable.get();
      return Reference.variable(variable);
    }

    throw new IllegalArgumentException();
  }

  private Reference getInstance(Class type) {
    RioComponent instance = rio.getInstance(type);
    return Reference.object(instance);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy