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.AvroConfigurationSerializer;
028import org.apache.reef.tang.formats.CommandLine;
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) final 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(final String[] args) throws Exception {
046    final Tang tang = Tang.Factory.getTang();
047    final JavaConfigurationBuilder cb = tang.newConfigurationBuilder();
048    final CommandLine cl = new CommandLine(cb);
049    cl.registerShortNameOfClass(Timer.Seconds.class);
050    cl.processCommandLine(args);
051    final Configuration conf = cb.build();
052    System.out.println("start conf");
053    final AvroConfigurationSerializer avroSerializer = new AvroConfigurationSerializer();
054    System.out.println(avroSerializer.toString(conf));
055    System.out.println("end conf");
056    final InjectorImpl injector = (InjectorImpl) tang.newInjector(conf);
057    final InjectionPlan<Timer> ip = injector.getInjectionPlan(Timer.class);
058    System.out.println(ip.toPrettyString());
059    System.out.println("Number of plans:" + ip.getNumAlternatives());
060    final Timer timer = injector.getInstance(Timer.class);
061    System.out.println("Tick...");
062    timer.sleep();
063    System.out.println("Tock.");
064  }
065
066  public void sleep() throws InterruptedException {
067    java.lang.Thread.sleep(seconds * 1000);
068  }
069
070  @NamedParameter(default_value = "10",
071      doc = "Number of seconds to sleep", short_name = "sec")
072  class Seconds implements Name<Integer> {
073  }
074}