Skip to content

Commit 54d37ea

Browse files
aietcnpivovarit
authored andcommitted
BAEL-658 (eugenp#4150)
1 parent 13d226c commit 54d37ea

File tree

12 files changed

+254
-0
lines changed

12 files changed

+254
-0
lines changed

autovalue/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,29 @@
1818
<artifactId>auto-value</artifactId>
1919
<version>${auto-value.version}</version>
2020
</dependency>
21+
<dependency>
22+
<groupId>com.google.auto.factory</groupId>
23+
<artifactId>auto-factory</artifactId>
24+
<version>${auto-factory.version}</version>
25+
<exclusions>
26+
<exclusion>
27+
<groupId>com.google.guava</groupId>
28+
<artifactId>guava</artifactId>
29+
</exclusion>
30+
</exclusions>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>com.google.inject</groupId>
35+
<artifactId>guice</artifactId>
36+
<version>${guice.version}</version>
37+
</dependency>
2138
</dependencies>
2239

2340
<properties>
2441
<auto-value.version>1.3</auto-value.version>
42+
<auto-factory.version>1.0-beta5</auto-factory.version>
43+
<guice.version>4.2.0</guice.version>
2544
</properties>
2645

2746
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.autofactory;
2+
3+
import com.baeldung.autofactory.model.Camera;
4+
import com.baeldung.autofactory.model.Phone;
5+
import com.baeldung.autofactory.model.PhoneFactory;
6+
import com.baeldung.autofactory.modules.SonyCameraModule;
7+
import com.google.inject.Guice;
8+
import com.google.inject.Injector;
9+
10+
public class App {
11+
12+
public static void main(String[] args) {
13+
PhoneFactory phoneFactory = new PhoneFactory(() -> new Camera("Unknown", "XXX"));
14+
Phone simplePhone = phoneFactory.create("other parts");
15+
16+
Injector injector = Guice.createInjector(new SonyCameraModule());
17+
PhoneFactory injectedFactory = injector.getInstance(PhoneFactory.class);
18+
Phone xperia = injectedFactory.create("Xperia");
19+
}
20+
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.autofactory;
2+
3+
import com.baeldung.autofactory.custom.SmartPhone;
4+
5+
/**
6+
* @author aiet
7+
*/
8+
public interface CustomStorage {
9+
10+
SmartPhone customROMInGB(int romSize);
11+
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.autofactory.custom;
2+
3+
/**
4+
* @author aiet
5+
*/
6+
public abstract class AbstractFactory {
7+
8+
abstract CustomPhone newInstance(String brand);
9+
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.autofactory.custom;
2+
3+
import com.google.auto.factory.AutoFactory;
4+
5+
/**
6+
* @author aiet
7+
*/
8+
@AutoFactory(extending = AbstractFactory.class)
9+
public class CustomPhone {
10+
11+
private final String brand;
12+
13+
public CustomPhone(String brand) {
14+
this.brand = brand;
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.autofactory.custom;
2+
3+
import com.baeldung.autofactory.CustomStorage;
4+
import com.google.auto.factory.AutoFactory;
5+
6+
/**
7+
* @author aiet
8+
*/
9+
@AutoFactory(className = "SamsungFactory", allowSubclasses = true, implementing = CustomStorage.class)
10+
public class SmartPhone {
11+
12+
private int romSize;
13+
14+
public SmartPhone(int romSize) {
15+
this.romSize = romSize;
16+
}
17+
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.autofactory.model;
2+
3+
/**
4+
* @author aiet
5+
*/
6+
public class Camera {
7+
8+
private final String manufacturer;
9+
private final String serial;
10+
11+
public Camera(String manufacturer, String serial) {
12+
this.manufacturer = manufacturer;
13+
this.serial = serial;
14+
}
15+
16+
public String getManufacturer() {
17+
return manufacturer;
18+
}
19+
20+
public String getSerial() {
21+
return serial;
22+
}
23+
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.autofactory.model;
2+
3+
import com.google.auto.factory.AutoFactory;
4+
import com.google.auto.factory.Provided;
5+
6+
/**
7+
* @author aiet
8+
*/
9+
public class ClassicPhone {
10+
11+
private final String dialpad;
12+
private final String ringer;
13+
private String otherParts;
14+
15+
@AutoFactory
16+
public ClassicPhone(@Provided String dialpad, @Provided String ringer) {
17+
this.dialpad = dialpad;
18+
this.ringer = ringer;
19+
}
20+
21+
@AutoFactory
22+
public ClassicPhone(String otherParts) {
23+
this("defaultDialPad", "defaultRinger");
24+
this.otherParts = otherParts;
25+
}
26+
27+
public String getDialpad() {
28+
return dialpad;
29+
}
30+
31+
public String getRinger() {
32+
return ringer;
33+
}
34+
35+
public String getOtherParts() {
36+
return otherParts;
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.autofactory.model;
2+
3+
import com.google.auto.factory.AutoFactory;
4+
import com.google.auto.factory.Provided;
5+
6+
import javax.inject.Named;
7+
8+
/**
9+
* @author aiet
10+
*/
11+
@AutoFactory
12+
public class Phone {
13+
14+
private Camera camera;
15+
private String otherParts;
16+
17+
public Phone(@Provided @Named("Sony") Camera camera, String otherParts) {
18+
this.camera = camera;
19+
this.otherParts = otherParts;
20+
}
21+
22+
/* required when used as a base class for AutoFactory */
23+
public Phone() {
24+
}
25+
26+
public Camera getCamera() {
27+
return camera;
28+
}
29+
30+
public String getOtherParts() {
31+
return otherParts;
32+
}
33+
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.autofactory.modules;
2+
3+
import com.baeldung.autofactory.model.Camera;
4+
import com.google.inject.AbstractModule;
5+
import com.google.inject.Provides;
6+
7+
import javax.inject.Named;
8+
9+
/**
10+
* @author aiet
11+
*/
12+
public class SonyCameraModule extends AbstractModule {
13+
14+
private static int SONY_CAMERA_SERIAL = 1;
15+
16+
@Named("Sony")
17+
@Provides
18+
Camera cameraProvider() {
19+
return new Camera("Sony", String.format("%03d", SONY_CAMERA_SERIAL++));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)