Skip to content

Commit f4f4fae

Browse files
Improve the code quality of the Registry class
Signed-off-by: AayushSaini101 <[email protected]>
1 parent 16e62f7 commit f4f4fae

File tree

2 files changed

+54
-34
lines changed

2 files changed

+54
-34
lines changed

src/main/java/land/oras/Registry.java

+52-32
Original file line numberDiff line numberDiff line change
@@ -172,41 +172,20 @@ public void deleteBlob(ContainerRef containerRef) {
172172
}
173173
handleError(response);
174174
}
175-
176-
/**
177-
* Upload an ORAS artifact
178-
* @param containerRef The container
179-
* @param paths The paths
180-
* @return The manifest
181-
*/
182-
public Manifest pushArtifact(ContainerRef containerRef, Path... paths) {
183-
return pushArtifact(containerRef, null, Annotations.empty(), Config.empty(), paths);
184-
}
185-
186-
/**
187-
* Upload an ORAS artifact
188-
* @param containerRef The container
189-
* @param artifactType The artifact type
190-
* @param paths The paths
191-
* @return The manifest
192-
*/
193-
public Manifest pushArtifact(ContainerRef containerRef, String artifactType, Path... paths) {
194-
return pushArtifact(containerRef, artifactType, Annotations.empty(), Config.empty(), paths);
195-
}
196-
197-
/**
198-
* Upload an ORAS artifact
199-
* @param containerRef The container
200-
* @param artifactType The artifact type
201-
* @param annotations The annotations
202-
* @param paths The paths
175+
/**
176+
* Push an ORAS artifact using the Builder pattern.
177+
* @param builder The PushArtifactBuilder containing all configuration
203178
* @return The manifest
204179
*/
205-
public Manifest pushArtifact(
206-
ContainerRef containerRef, String artifactType, Annotations annotations, Path... paths) {
207-
return pushArtifact(containerRef, artifactType, annotations, Config.empty(), paths);
180+
public Manifest pushArtifact(PushArtifactBuilder builder) {
181+
return pushArtifact(
182+
builder.containerRef,
183+
builder.artifactType,
184+
builder.annotations,
185+
builder.config,
186+
builder.paths
187+
);
208188
}
209-
210189
/**
211190
* Download an ORAS artifact
212191
* @param containerRef The container
@@ -238,6 +217,7 @@ public void pullArtifact(ContainerRef containerRef, Path path, boolean overwrite
238217
}
239218
}
240219

220+
241221
/**
242222
* Upload an ORAS artifact
243223
* @param containerRef The container
@@ -300,6 +280,46 @@ public Manifest pushArtifact(
300280
LOG.debug("Manifest pushed to: {}", location);
301281
return manifest;
302282
}
283+
/**
284+
* Builder class for configuring artifact uploads.
285+
*/
286+
public static class PushArtifactBuilder {
287+
private final ContainerRef containerRef; // Required parameter
288+
private String artifactType = null; // Optional parameter
289+
private Annotations annotations = Annotations.empty(); // Default value
290+
private Config config = Config.empty(); // Default value
291+
private Path[] paths; // Required parameter
292+
293+
// Constructor for required fields
294+
public PushArtifactBuilder(ContainerRef containerRef, Path... paths) {
295+
this.containerRef = containerRef;
296+
this.paths = paths;
297+
}
298+
299+
// Setter for artifact type
300+
public PushArtifactBuilder withArtifactType(String artifactType) {
301+
this.artifactType = artifactType;
302+
return this;
303+
}
304+
305+
// Setter for annotations
306+
public PushArtifactBuilder withAnnotations(Annotations annotations) {
307+
this.annotations = annotations;
308+
return this;
309+
}
310+
311+
// Setter for config
312+
public PushArtifactBuilder withConfig(Config config) {
313+
this.config = config;
314+
return this;
315+
}
316+
317+
// Build method (optional if you just want chaining)
318+
public PushArtifactBuilder build() {
319+
return this;
320+
}
321+
}
322+
303323

304324
/**
305325
* Push a blob from file

src/test/java/land/oras/RegistryTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ void testShouldPushAndPullMinimalArtifact() throws IOException {
184184
Files.writeString(file1, "foobar");
185185

186186
// Upload
187-
Manifest manifest = registry.pushArtifact(containerRef, file1);
187+
Manifest manifest = registry.pushArtifact(new Registry.PushArtifactBuilder(containerRef, file1));
188188
assertEquals(1, manifest.getLayers().size());
189189

190190
Layer layer = manifest.getLayers().get(0);
@@ -223,7 +223,7 @@ void testShouldPushCompressedDirectory() throws IOException {
223223
Files.writeString(file3, "barfoo");
224224

225225
// Upload blob dir
226-
Manifest manifest = registry.pushArtifact(containerRef, blobDir);
226+
Manifest manifest = registry.pushArtifact(new Registry.PushArtifactBuilder(containerRef, blobDir));
227227
assertEquals(1, manifest.getLayers().size());
228228

229229
Layer layer = manifest.getLayers().get(0);

0 commit comments

Comments
 (0)