Skip to content

Instantly share code, notes, and snippets.

@martint
Created September 10, 2014 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martint/134ae742c980d56cf774 to your computer and use it in GitHub Desktop.
Save martint/134ae742c980d56cf774 to your computer and use it in GitHub Desktop.
/*
* 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 com.facebook.presto;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.google.inject.multibindings.MapBinder;
import org.weakref.jmx.guice.ExportBinder;
import javax.inject.Inject;
import java.util.Arrays;
import java.util.Map;
public class Main
{
public static void main(String[] args)
{
Injector injector = Guice.createInjector(new Module()
{
@Override
public void configure(Binder binder)
{
ExportBinder exportBinder = ExportBinder.newExporter(binder);
MapBinder<String, Stats> mapBinder = MapBinder.newMapBinder(binder, String.class, Stats.class);
int i = 0;
for (Class<? extends Stats> clazz : Arrays.asList(Stats1.class, Stats2.class, Stats3.class)) {
binder.bind(clazz).in(Scopes.SINGLETON);
mapBinder.addBinding("stats" + i).to(Stats1.class);
exportBinder.export(clazz).withGeneratedName();
i++;
}
binder.bind(Updater1.class).in(Scopes.SINGLETON);
binder.bind(Updater2.class).in(Scopes.SINGLETON);
binder.bind(Reporter.class).in(Scopes.SINGLETON);
}
});
Reporter reporter = injector.getInstance(Reporter.class);
}
public static interface Stats
{
}
public static class Stats1 implements Stats {}
public static class Stats2 implements Stats {}
public static class Stats3 implements Stats {}
public static class Updater1
{
@Inject
public Updater1(Stats1 stats) {}
}
public static class Updater2
{
@Inject
public Updater2(Stats2 stats2, Stats3 stats3) {}
}
public static class Reporter
{
@Inject
public Reporter(Map<String, Stats> stats)
{
System.out.println(stats);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment