2020
2121package land .oras ;
2222
23- import com .fasterxml .jackson .annotation .JsonCreator ;
24- import com .fasterxml .jackson .annotation .JsonGetter ;
25- import com .fasterxml .jackson .annotation .JsonIgnore ;
23+ import com .fasterxml .jackson .annotation .JsonProperty ;
2624import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
27- import java .util .Map ;
25+ import java .util .List ;
26+ import java .util .Objects ;
2827import land .oras .utils .Const ;
2928import org .jspecify .annotations .NullMarked ;
3029import org .jspecify .annotations .Nullable ;
3130
3231/**
3332 * Record for platform information
34- * @param annotations Platform annotations, which can include os and architecture information
33+ * @param os The operating system of the platform
34+ * @param architecture The architecture of the platform
35+ * @param variant The variant of the platform, which is optional and may be null
36+ * @param osVersion The operating system version of the platform, which is optional and may be
37+ * @param features The features of the platform, which is optional and may be null
38+ * @param osFeatures The operating system features of the platform, which is optional and may be
3539 */
3640@ NullMarked
3741@ OrasModel
38- @ JsonPropertyOrder ({Const .PLATFORM_OS , Const .PLATFORM_ARCHITECTURE })
39- public record Platform (@ Nullable @ JsonIgnore Map <String , String > annotations ) {
42+ @ JsonPropertyOrder ({
43+ Const .PLATFORM_OS ,
44+ Const .PLATFORM_ARCHITECTURE ,
45+ Const .PLATFORM_VARIANT ,
46+ Const .PLATFORM_OS_VERSION ,
47+ Const .PLATFORM_OS_FEATURES
48+ })
49+ public record Platform (
50+ @ Nullable @ JsonProperty (Const .PLATFORM_OS ) String os ,
51+ @ Nullable @ JsonProperty (Const .PLATFORM_ARCHITECTURE ) String architecture ,
52+ @ Nullable @ JsonProperty (Const .PLATFORM_OS_VERSION ) String osVersion ,
53+ @ Nullable @ JsonProperty (Const .PLATFORM_VARIANT ) String variant ,
54+ @ Nullable @ JsonProperty (Const .PLATFORM_FEATURES ) List <String > features ,
55+ @ Nullable @ JsonProperty (Const .PLATFORM_OS_FEATURES ) List <String > osFeatures ) {
4056
4157 /**
42- * Constructor for JSON deserialization
43- * @param annotations Platform annotations, which can include os and architecture information
58+ * Create a new platform linux/amd64
59+ * @return The platform
4460 */
45- @ JsonCreator
46- public Platform {}
61+ public static Platform linuxAmd64 () {
62+ return of (Const .PLATFORM_LINUX , Const .PLATFORM_ARCHITECTURE_AMD64 );
63+ }
4764
4865 /**
49- * Create a new platform with the given annotations
50- * @param annotations Platform annotations, which can include os and architecture information
66+ * Create a new platform windows/amd64
5167 * @return The platform
5268 */
53- public static Platform of ( @ Nullable Map < String , String > annotations ) {
54- return new Platform ( annotations );
69+ public static Platform windowsAmd64 ( ) {
70+ return of ( Const . PLATFORM_WINDOWS , Const . PLATFORM_ARCHITECTURE_AMD64 );
5571 }
5672
5773 /**
5874 * Create a new platform linux/amd64
5975 * @return The platform
6076 */
61- public static Platform linuxAmd64 () {
62- return of (Const .PLATFORM_LINUX , Const .PLATFORM_ARCHITECTURE_AMD64 );
77+ public static Platform linux386 () {
78+ return of (Const .PLATFORM_LINUX , Const .PLATFORM_ARCHITECTURE_386 );
79+ }
80+
81+ /**
82+ * Create a new platform linux arm/v6
83+ * @return The platform
84+ */
85+ public static Platform linuxArmV6 () {
86+ return of (Const .PLATFORM_LINUX , Const .PLATFORM_ARCHITECTURE_ARM , Const .VARIANT_V6 );
87+ }
88+
89+ /**
90+ * Create a new platform linux arm/v7
91+ * @return The platform
92+ */
93+ public static Platform linuxArmV7 () {
94+ return of (Const .PLATFORM_LINUX , Const .PLATFORM_ARCHITECTURE_ARM , Const .VARIANT_V7 );
95+ }
96+
97+ /**
98+ * Create a new platform linux arm64/v8
99+ * @return The platform
100+ */
101+ public static Platform linuxArm64V8 () {
102+ return of (Const .PLATFORM_LINUX , Const .PLATFORM_ARCHITECTURE_ARM64 , Const .VARIANT_V8 );
103+ }
104+
105+ /**
106+ * Create a new platform ppc64le
107+ * @return The platform
108+ */
109+ public static Platform linuxPpc64le () {
110+ return of (Const .PLATFORM_LINUX , Const .PLATFORM_ARCHITECTURE_PPC64LE , null );
111+ }
112+
113+ /**
114+ * Create a new platform riscv64
115+ * @return The platform
116+ */
117+ public static Platform linuxRiscv64 () {
118+ return of (Const .PLATFORM_LINUX , Const .PLATFORM_ARCHITECTURE_RISCV64 , null );
119+ }
120+
121+ /**
122+ * Create a new platform s390x
123+ * @return The platform
124+ */
125+ public static Platform linuxS390x () {
126+ return of (Const .PLATFORM_LINUX , Const .PLATFORM_ARCHITECTURE_S390X , null );
63127 }
64128
65129 /**
66130 * Create a new platform with empty os and architecture
67131 * @return The platform
68132 */
69133 public static Platform empty () {
70- return new Platform (null );
134+ return new Platform (null , null , null , null , null , null );
71135 }
72136
73137 /**
@@ -85,7 +149,7 @@ public static Platform unknown() {
85149 * @return The platform
86150 */
87151 public static Platform of (String os , String architecture ) {
88- return new Platform (Map . of ( Const . PLATFORM_OS , os , Const . PLATFORM_ARCHITECTURE , architecture ) );
152+ return new Platform (os , architecture , null , null , null , null );
89153 }
90154
91155 /**
@@ -96,44 +160,61 @@ public static Platform of(String os, String architecture) {
96160 * @return The platform
97161 */
98162 public static Platform of (String os , String architecture , @ Nullable String variant ) {
99- if (variant == null ) {
100- return of (os , architecture );
101- }
102- return new Platform (Map .of (
103- Const .PLATFORM_OS , os ,
104- Const .PLATFORM_ARCHITECTURE , architecture ,
105- Const .PLATFORM_VARIANT , variant ));
163+ return new Platform (os , architecture , null , variant , null , null );
106164 }
107165
108166 /**
109- * Return the architecture of the platform, or "unknown" if not specified
110- * @return The architecture of the platform
167+ * Return the os of the platform, or "unknown" if the os is null
168+ * @return The os of the platform
111169 */
112- @ JsonGetter
170+ @ Override
113171 public String os () {
114- return annotations != null
115- ? annotations .getOrDefault (Const .PLATFORM_OS , Const .PLATFORM_UNKNOWN )
116- : Const .PLATFORM_UNKNOWN ;
172+ return os != null ? os : Const .PLATFORM_UNKNOWN ;
117173 }
118174
119175 /**
120- * Return the architecture of the platform, or "unknown" if not specified
121- * @return The architecture of the platform
176+ * Return the os of the platform, or "unknown" if the os is null
177+ * @return The os of the platform
122178 */
123- @ JsonGetter
179+ @ Override
124180 public String architecture () {
125- return annotations != null
126- ? annotations .getOrDefault (Const .PLATFORM_ARCHITECTURE , Const .PLATFORM_UNKNOWN )
127- : Const .PLATFORM_UNKNOWN ;
181+ return architecture != null ? architecture : Const .PLATFORM_UNKNOWN ;
182+ }
183+
184+ /**
185+ * Create a new platform with the given features
186+ * @param features The features of the platform
187+ * @return The platform
188+ */
189+ public Platform withFeatures (List <String > features ) {
190+ return new Platform (os , architecture , osVersion , variant , features , osFeatures );
191+ }
192+
193+ /**
194+ * Create a new platform with the given variant
195+ * @param variant The variant of the platform
196+ * @return The platform
197+ */
198+ public Platform withVariant (String variant ) {
199+ return new Platform (os , architecture , osVersion , variant , features , osFeatures );
200+ }
201+
202+ /**
203+ * Create a new platform with the given os version
204+ * @param osVersion The os version of the platform
205+ * @return The platform
206+ */
207+ public Platform withOsVersion (String osVersion ) {
208+ return new Platform (os , architecture , osVersion , variant , features , osFeatures );
128209 }
129210
130211 /**
131- * Return the variant of the platform, or null if not specified
132- * @return The variant of the platform
212+ * Create a new platform with the given os features
213+ * @param osFeatures The os features of the platform
214+ * @return The platform
133215 */
134- @ JsonGetter
135- public @ Nullable String variant () {
136- return annotations != null ? annotations .get (Const .PLATFORM_VARIANT ) : null ;
216+ public Platform withOsFeatures (List <String > osFeatures ) {
217+ return new Platform (os , architecture , osVersion , variant , features , osFeatures );
137218 }
138219
139220 /**
@@ -144,4 +225,34 @@ public String architecture() {
144225 public static boolean unspecified (Platform platform ) {
145226 return platform .equals (Platform .empty ()) || platform .equals (Platform .unknown ());
146227 }
228+
229+ /**
230+ * Check if 2 platform are matching, which means the os and architecture are the same (including variant)
231+ * @param platform The platform to check
232+ * @param target The target platform to match
233+ * @return True if the platform is matching, false otherwise
234+ */
235+ public static boolean matches (Platform platform , Platform target ) {
236+ return matches (platform , target , false );
237+ }
238+
239+ /**
240+ * Check if 2 platform are matching, which means the os and architecture are the same (including variant)
241+ * @param platform The platform to check
242+ * @param target The target platform to match
243+ * @param includeVersion Whether to include os version
244+ * @return True if the platform is matching, false otherwise
245+ */
246+ public static boolean matches (Platform platform , Platform target , boolean includeVersion ) {
247+ if (!platform .os ().equals (target .os ()) || !platform .architecture ().equals (target .architecture ())) {
248+ return false ;
249+ }
250+ if (!Objects .equals (platform .variant (), target .variant ())) {
251+ return false ;
252+ }
253+ if (includeVersion ) {
254+ return Objects .equals (platform .osVersion (), target .osVersion ());
255+ }
256+ return true ;
257+ }
147258}
0 commit comments