org.apache.reef.tang.annotations.NamedParameter Java Examples

The following examples show how to use org.apache.reef.tang.annotations.NamedParameter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProtocolBufferClassHierarchy.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public ClassHierarchy merge(final ClassHierarchy ch) {
  if (this == ch) {
    return this;
  }
  if (!(ch instanceof ProtocolBufferClassHierarchy)) {
    throw new UnsupportedOperationException(
        "Cannot merge with class hierarchies of type: " + ch.getClass().getName());
  }

  final ProtocolBufferClassHierarchy pch = (ProtocolBufferClassHierarchy) ch;
  for (final String key : pch.lookupTable.keySet()) {
    if (!this.lookupTable.containsKey(key)) {
      this.lookupTable.put(key, pch.lookupTable.get(key));
    }

    for (final Node n : ch.getNamespace().getChildren()) {
      if (!this.namespace.contains(n.getFullName())) {
        if (n instanceof NamedParameter) {
          final NamedParameterNode np = (NamedParameterNode) n;
          new NamedParameterNodeImpl<>(this.namespace, np.getName(),
              np.getFullName(), np.getFullArgName(), np.getSimpleArgName(),
              np.isSet(), np.isList(), np.getDocumentation(), np.getShortName(),
              np.getDefaultInstanceAsStrings());
        } else if (n instanceof ClassNode) {
          final ClassNode cn = (ClassNode) n;
          new ClassNodeImpl(namespace, cn.getName(), cn.getFullName(),
              cn.isUnit(), cn.isInjectionCandidate(),
              cn.isExternalConstructor(), cn.getInjectableConstructors(),
              cn.getAllConstructors(), cn.getDefaultImplementation());
        }
      }
    }
  }
  return this;
}
 
Example #2
Source File: AvroClassHierarchy.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public ClassHierarchy merge(final ClassHierarchy ch) {
  if (this == ch) {
    return this;
  }
  if (!(ch instanceof AvroClassHierarchy)) {
    throw new UnsupportedOperationException(
            "Cannot merge with class hierarchies of type: " + ch.getClass().getName());
  }

  final AvroClassHierarchy ach = (AvroClassHierarchy) ch;
  for (final String key : ach.lookupTable.keySet()) {
    if (!this.lookupTable.containsKey(key)) {
      this.lookupTable.put(key, ach.lookupTable.get(key));
    }
  }

  for (final Node n : ch.getNamespace().getChildren()) {
    if (!this.namespace.contains(n.getFullName())) {
      if (n instanceof NamedParameter) {
        final NamedParameterNode np = (NamedParameterNode) n;
        new NamedParameterNodeImpl<>(this.namespace, np.getName(), np.getFullName(), np.getFullArgName(),
                np.getSimpleArgName(), np.isSet(), np.isList(), np.getDocumentation(), np.getShortName(),
                np.getDefaultInstanceAsStrings());
      } else if (n instanceof ClassNode) {
        final ClassNode cn = (ClassNode) n;
        new ClassNodeImpl<>(namespace, cn.getName(), cn.getFullName(), cn.isUnit(), cn.isInjectionCandidate(),
                cn.isExternalConstructor(), cn.getInjectableConstructors(), cn.getAllConstructors(),
                cn.getDefaultImplementation());
      }
    }
  }
  return this;
}
 
Example #3
Source File: RandomNameCNS.java    From reef with Apache License 2.0 4 votes vote down vote up
@Deprecated
public RandomNameCNS(@Parameter(PREFIX.class) final String prefix) {
  this.prefix = prefix;
  this.lengthOfRandomSuffix
          = Integer.parseInt(LengthOfRandomSuffix.class.getAnnotation(NamedParameter.class).default_value());
}
 
Example #4
Source File: ClassHierarchyImpl.java    From reef with Apache License 2.0 4 votes vote down vote up
private <T, U> Node buildPathToNode(final Class<U> clazz)
    throws ClassHierarchyException {
  final String[] path = clazz.getName().split("\\$");

  Node root = namespace;
  for (int i = 0; i < path.length - 1; i++) {
    root = root.get(path[i]);
  }

  if (root == null) {
    throw new NullPointerException("The root of the path to node is null. "
            + "The class name is likely to be malformed. The clazz.getName() returns " + clazz.getName());
  }
  final Node parent = root;

  final Type argType = ReflectionUtilities.getNamedParameterTargetOrNull(clazz);

  if (argType == null) {
    return JavaNodeFactory.createClassNode(parent, clazz);
  } else {

    // checked inside of NamedParameterNode, using reflection.
    @SuppressWarnings("unchecked") final NamedParameterNode<T> np = JavaNodeFactory.createNamedParameterNode(
        parent, (Class<? extends Name<T>>) clazz, argType);

    if (parameterParser.canParse(ReflectionUtilities.getFullName(argType)) &&
        clazz.getAnnotation(NamedParameter.class).default_class() != Void.class) {
      throw new ClassHierarchyException("Named parameter " + ReflectionUtilities.getFullName(clazz) +
          " defines default implementation for parsable type " + ReflectionUtilities.getFullName(argType));
    }

    final String shortName = np.getShortName();
    if (shortName != null) {
      final NamedParameterNode<?> oldNode = shortNames.get(shortName);
      if (oldNode != null) {
        if (oldNode.getFullName().equals(np.getFullName())) {
          throw new IllegalStateException("Tried to double bind "
              + oldNode.getFullName() + " to short name " + shortName);
        }
        throw new ClassHierarchyException("Named parameters " + oldNode.getFullName()
            + " and " + np.getFullName() + " have the same short name: "
            + shortName);
      }
      shortNames.put(shortName, np);
    }
    return np;
  }
}