|
16 | 16 |
|
17 | 17 | package com.google.cloud.tools.jib.image; |
18 | 18 |
|
19 | | -import java.util.ArrayList; |
20 | | -import java.util.Collections; |
| 19 | +import com.google.common.collect.ImmutableList; |
21 | 20 | import java.util.List; |
22 | 21 | import java.util.Map; |
23 | | -import javax.annotation.Nullable; |
24 | 22 |
|
25 | 23 | /** Represents an image. */ |
26 | 24 | public class Image { |
27 | 25 |
|
28 | | - /** The layers of the image, in the order in which they are applied. */ |
29 | | - private ImageLayers<Layer> layers = new ImageLayers<>(); |
| 26 | + public static class Builder { |
30 | 27 |
|
31 | | - /** Environment variable definitions for running the image, in the format {@code NAME=VALUE}. */ |
32 | | - private final List<String> environment = new ArrayList<>(); |
| 28 | + private final ImageLayers.Builder<Layer> imageLayersBuilder = ImageLayers.builder(); |
| 29 | + private final ImmutableList.Builder<String> environmentBuilder = ImmutableList.builder(); |
33 | 30 |
|
34 | | - /** Initial command to run when running the image. */ |
35 | | - @Nullable private List<String> entrypoint; |
| 31 | + private ImmutableList<String> entrypoint = ImmutableList.of(); |
36 | 32 |
|
37 | | - public List<String> getEnvironment() { |
38 | | - return Collections.unmodifiableList(environment); |
39 | | - } |
| 33 | + /** Sets the environment with a map from environment variable names to values. */ |
| 34 | + public Builder setEnvironment(Map<String, String> environment) { |
| 35 | + for (Map.Entry<String, String> environmentVariable : environment.entrySet()) { |
| 36 | + setEnvironmentVariable(environmentVariable.getKey(), environmentVariable.getValue()); |
| 37 | + } |
| 38 | + return this; |
| 39 | + } |
40 | 40 |
|
41 | | - /** Sets the environment with a map from environment variable names to values. */ |
42 | | - public Image setEnvironment(Map<String, String> environment) { |
43 | | - for (Map.Entry<String, String> environmentVariable : environment.entrySet()) { |
44 | | - setEnvironmentVariable(environmentVariable.getKey(), environmentVariable.getValue()); |
| 41 | + public Builder setEnvironmentVariable(String name, String value) { |
| 42 | + environmentBuilder.add(name + "=" + value); |
| 43 | + return this; |
45 | 44 | } |
46 | | - return this; |
47 | | - } |
48 | 45 |
|
49 | | - public Image setEnvironmentVariable(String name, String value) { |
50 | | - environment.add(name + "=" + value); |
51 | | - return this; |
52 | | - } |
| 46 | + /** Adds an environment variable definition in the format {@code NAME=VALUE}. */ |
| 47 | + public Builder addEnvironmentVariableDefinition(String environmentVariableDefinition) { |
| 48 | + environmentBuilder.add(environmentVariableDefinition); |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + public Builder setEntrypoint(List<String> entrypoint) { |
| 53 | + this.entrypoint = ImmutableList.copyOf(entrypoint); |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + public Builder addLayer(Layer layer) throws LayerPropertyNotFoundException { |
| 58 | + imageLayersBuilder.add(layer); |
| 59 | + return this; |
| 60 | + } |
53 | 61 |
|
54 | | - /** Adds an environment variable definition in the format {@code NAME=VALUE}. */ |
55 | | - public Image addEnvironmentVariableDefinition(String environmentVariableDefinition) { |
56 | | - environment.add(environmentVariableDefinition); |
57 | | - return this; |
| 62 | + public <T extends Layer> Builder addLayers(ImageLayers<T> layers) |
| 63 | + throws LayerPropertyNotFoundException { |
| 64 | + this.imageLayersBuilder.addAll(layers); |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + public Image build() { |
| 69 | + return new Image( |
| 70 | + imageLayersBuilder.build(), environmentBuilder.build(), ImmutableList.copyOf(entrypoint)); |
| 71 | + } |
58 | 72 | } |
59 | 73 |
|
60 | | - public List<String> getEntrypoint() { |
61 | | - return Collections.unmodifiableList(entrypoint); |
| 74 | + public static Builder builder() { |
| 75 | + return new Builder(); |
62 | 76 | } |
63 | 77 |
|
64 | | - public Image setEntrypoint(@Nullable List<String> entrypoint) { |
| 78 | + /** The layers of the image, in the order in which they are applied. */ |
| 79 | + private final ImageLayers<Layer> layers; |
| 80 | + |
| 81 | + /** Environment variable definitions for running the image, in the format {@code NAME=VALUE}. */ |
| 82 | + private final ImmutableList<String> environmentBuilder; |
| 83 | + |
| 84 | + /** Initial command to run when running the image. */ |
| 85 | + private final ImmutableList<String> entrypoint; |
| 86 | + |
| 87 | + private Image( |
| 88 | + ImageLayers<Layer> layers, |
| 89 | + ImmutableList<String> environment, |
| 90 | + ImmutableList<String> entrypoint) { |
| 91 | + this.layers = layers; |
| 92 | + this.environmentBuilder = environment; |
65 | 93 | this.entrypoint = entrypoint; |
66 | | - return this; |
67 | 94 | } |
68 | 95 |
|
69 | | - public List<Layer> getLayers() { |
70 | | - return layers.getLayers(); |
| 96 | + public ImmutableList<String> getEnvironment() { |
| 97 | + return environmentBuilder; |
71 | 98 | } |
72 | 99 |
|
73 | | - public Image addLayer(Layer layer) throws LayerPropertyNotFoundException { |
74 | | - layers.add(layer); |
75 | | - return this; |
| 100 | + public ImmutableList<String> getEntrypoint() { |
| 101 | + return entrypoint; |
76 | 102 | } |
77 | 103 |
|
78 | | - public <T extends Layer> Image addLayers(ImageLayers<T> layers) |
79 | | - throws LayerPropertyNotFoundException { |
80 | | - this.layers.addAll(layers); |
81 | | - return this; |
| 104 | + public ImmutableList<Layer> getLayers() { |
| 105 | + return layers.getLayers(); |
82 | 106 | } |
83 | 107 | } |
0 commit comments