Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-organize guice examples #1180

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading