Skip to content

Commit

Permalink
re-organize guice examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kainagel committed Nov 4, 2024
1 parent d296d3c commit 429d35d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.matsim.codeexamples.guicewithoutmatsim;
package org.matsim.codeexamples.guicewithoutmatsim.aaConventionalConstructorDI;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

class ConventionalInjectionByConstructor{

private static final Logger log = LogManager.getLogger( BasicInjection.class ) ;
private static final Logger log = LogManager.getLogger( ConventionalInjectionByConstructor.class ) ;

public static void main(String[] args){
Helper helper = new MyHelper1();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.matsim.codeexamples.guicewithoutmatsim;
package org.matsim.codeexamples.guicewithoutmatsim.bbBasicGuiceDI;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.multibindings.MapBinder;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

Expand All @@ -15,21 +16,12 @@ public static void main(String[] args){
@Override protected void configure(){
bind(Simulation.class).to( MySimulation1.class ) ;
bind( Helper.class ).to( MyHelper1.class ) ;
// MapBinder<String,TravelTime>.newMapBinder( this, "car", )
}
} );
Simulation sim = injector.getInstance(Simulation.class);
sim.run() ;
}

interface Simulation {
void run() ;
}

interface Helper {
Object getAccessToSomething() ;
}

static class MySimulation1 implements Simulation {
// arguments that would normally be in the constructor can now be obtained via @Inject !
@Inject Helper helper ;
Expand Down Expand Up @@ -58,4 +50,13 @@ static class MyHelper2 implements Helper {
return null ;
}
}

interface Simulation {
void run() ;
}

interface Helper {
Object getAccessToSomething() ;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.matsim.codeexamples.guicewithoutmatsim;
package org.matsim.codeexamples.guicewithoutmatsim.ccMapBinder;

import com.google.inject.*;
import com.google.inject.multibindings.MapBinder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.matsim.codeexamples.guicewithoutmatsim;
package org.matsim.codeexamples.guicewithoutmatsim.ccMapBinder;

import com.google.inject.*;
import com.google.inject.multibindings.MapBinder;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.poi.ss.formula.functions.T;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -45,12 +46,18 @@ protected void configure(){
}
log.info("") ;

Map<String, Provider<MyInterface>> map = injector.getInstance( Key.get( new TypeLiteral<Map<String, Provider<MyInterface>>>(){} ) );;
Provider<MyInterface> provider = map.get( "abc" );;
{
Map<String, MyInterface> map = injector.getInstance( Key.get( new TypeLiteral<Map<String, MyInterface>>(){
} ) );
MyInterface result = map.get( "abc" );
}
{
Map<String, Provider<MyInterface>> map = injector.getInstance( Key.get( new TypeLiteral<Map<String, Provider<MyInterface>>>(){

} ) );
Provider<MyInterface> provider = map.get( "abc" );
}

// for( Provider<MyInterface> provider : set ){
provider.get() ;
// }

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.matsim.codeexamples.guicewithoutmatsim;
package org.matsim.codeexamples.guicewithoutmatsim.ddMultiBinder;

import java.util.ArrayList;
import java.util.Collection;
Expand Down

0 comments on commit 429d35d

Please sign in to comment.