|
| 1 | +/*- |
| 2 | + * =LICENSE= |
| 3 | + * ORAS Java SDK |
| 4 | + * === |
| 5 | + * Copyright (C) 2024 - 2026 ORAS |
| 6 | + * === |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * =LICENSEEND= |
| 19 | + */ |
| 20 | + |
| 21 | +package land.oras; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 24 | +import com.fasterxml.jackson.annotation.JsonGetter; |
| 25 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 26 | +import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
| 27 | +import java.util.Map; |
| 28 | +import land.oras.utils.Const; |
| 29 | +import org.jspecify.annotations.NullMarked; |
| 30 | +import org.jspecify.annotations.Nullable; |
| 31 | + |
| 32 | +/** |
| 33 | + * Record for platform information |
| 34 | + * @param annotations Platform annotations, which can include os and architecture information |
| 35 | + */ |
| 36 | +@NullMarked |
| 37 | +@OrasModel |
| 38 | +@JsonPropertyOrder({Const.PLATFORM_OS, Const.PLATFORM_ARCHITECTURE}) |
| 39 | +public record Platform(@Nullable @JsonIgnore Map<String, String> annotations) { |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructor for JSON deserialization |
| 43 | + * @param annotations Platform annotations, which can include os and architecture information |
| 44 | + */ |
| 45 | + @JsonCreator |
| 46 | + public Platform {} |
| 47 | + |
| 48 | + /** |
| 49 | + * Create a new platform with the given annotations |
| 50 | + * @param annotations Platform annotations, which can include os and architecture information |
| 51 | + * @return The platform |
| 52 | + */ |
| 53 | + public static Platform of(@Nullable Map<String, String> annotations) { |
| 54 | + return new Platform(annotations); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Create a new platform linux/amd64 |
| 59 | + * @return The platform |
| 60 | + */ |
| 61 | + public static Platform linuxAmd64() { |
| 62 | + return of(Const.PLATFORM_LINUX, Const.PLATFORM_ARCHITECTURE_AMD64); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create a new platform with empty os and architecture |
| 67 | + * @return The platform |
| 68 | + */ |
| 69 | + public static Platform empty() { |
| 70 | + return new Platform(null); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Create a new platform with unknown os and architecture |
| 75 | + * @return The platform |
| 76 | + */ |
| 77 | + public static Platform unknown() { |
| 78 | + return of(Const.PLATFORM_UNKNOWN, Const.PLATFORM_UNKNOWN); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Create a new platform with the given os and architecture |
| 83 | + * @param os The os of the platform |
| 84 | + * @param architecture The architecture of the platform |
| 85 | + * @return The platform |
| 86 | + */ |
| 87 | + public static Platform of(String os, String architecture) { |
| 88 | + return new Platform(Map.of(Const.PLATFORM_OS, os, Const.PLATFORM_ARCHITECTURE, architecture)); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Create a new platform with the given os, architecture and variant |
| 93 | + * @param os The os of the platform |
| 94 | + * @param architecture The architecture of the platform |
| 95 | + * @param variant The variant of the platform |
| 96 | + * @return The platform |
| 97 | + */ |
| 98 | + 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)); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Return the architecture of the platform, or "unknown" if not specified |
| 110 | + * @return The architecture of the platform |
| 111 | + */ |
| 112 | + @JsonGetter |
| 113 | + public String os() { |
| 114 | + return annotations != null |
| 115 | + ? annotations.getOrDefault(Const.PLATFORM_OS, Const.PLATFORM_UNKNOWN) |
| 116 | + : Const.PLATFORM_UNKNOWN; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Return the architecture of the platform, or "unknown" if not specified |
| 121 | + * @return The architecture of the platform |
| 122 | + */ |
| 123 | + @JsonGetter |
| 124 | + public String architecture() { |
| 125 | + return annotations != null |
| 126 | + ? annotations.getOrDefault(Const.PLATFORM_ARCHITECTURE, Const.PLATFORM_UNKNOWN) |
| 127 | + : Const.PLATFORM_UNKNOWN; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Return the variant of the platform, or null if not specified |
| 132 | + * @return The variant of the platform |
| 133 | + */ |
| 134 | + @JsonGetter |
| 135 | + public @Nullable String variant() { |
| 136 | + return annotations != null ? annotations.get(Const.PLATFORM_VARIANT) : null; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Return true if the platform is unspecified, which means both os and architecture are either empty or unknown |
| 141 | + * @param platform The platform to check |
| 142 | + * @return True if the platform is unspecified, false otherwise |
| 143 | + */ |
| 144 | + public static boolean unspecified(Platform platform) { |
| 145 | + return platform.equals(Platform.empty()) || platform.equals(Platform.unknown()); |
| 146 | + } |
| 147 | +} |
0 commit comments