This project has retired. For details please refer to its Attic page.
Source code
001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.tang.examples;
020
021import org.apache.reef.tang.Configuration;
022import org.apache.reef.tang.JavaConfigurationBuilder;
023import org.apache.reef.tang.Tang;
024import org.apache.reef.tang.annotations.Name;
025import org.apache.reef.tang.annotations.NamedParameter;
026import org.apache.reef.tang.annotations.Parameter;
027import org.apache.reef.tang.formats.CommandLine;
028import org.apache.reef.tang.formats.ConfigurationFile;
029import org.apache.reef.tang.implementation.InjectionPlan;
030import org.apache.reef.tang.implementation.java.InjectorImpl;
031
032import javax.inject.Inject;
033
034public class Timer {
035  private final int seconds;
036
037  @Inject
038  public Timer(@Parameter(Seconds.class) int seconds) {
039    if (seconds < 0) {
040      throw new IllegalArgumentException("Cannot sleep for negative time!");
041    }
042    this.seconds = seconds;
043  }
044
045  public static void main(String[] args) throws Exception {
046    Tang tang = Tang.Factory.getTang();
047    JavaConfigurationBuilder cb = tang.newConfigurationBuilder();
048    CommandLine cl = new CommandLine(cb);
049    cl.registerShortNameOfClass(Timer.Seconds.class);
050    cl.processCommandLine(args);
051    Configuration conf = cb.build();
052    System.out.println("start conf");
053    System.out.println(ConfigurationFile.toConfigurationString(conf));
054    System.out.println("end conf");
055    InjectorImpl injector = (InjectorImpl) tang.newInjector(conf);
056    InjectionPlan<Timer> ip = injector.getInjectionPlan(Timer.class);
057    System.out.println(ip.toPrettyString());
058    System.out.println("Number of plans:" + ip.getNumAlternatives());
059    Timer timer = injector.getInstance(Timer.class);
060    System.out.println("Tick...");
061    timer.sleep();
062    System.out.println("Tock.");
063  }
064
065  public void sleep() throws InterruptedException {
066    java.lang.Thread.sleep(seconds * 1000);
067  }
068
069  @NamedParameter(default_value = "10",
070      doc = "Number of seconds to sleep", short_name = "sec")
071  class Seconds implements Name<Integer> {
072  }
073}