package org.openautonomousconnection.protocol.versions; import lombok.Getter; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Enum representing different protocol versions, their types, sides, and compatibility. */ public enum ProtocolVersion implements Serializable { /** * Support for old OAC-Project => *_old */ PV_1_0_0_CLASSIC("1.0.0", ProtocolType.CLASSIC, ProtocolSide.WEB_DNS, List.of(Protocol.HTTP)), /** * First Beta Version of OAC-Protocol */ PV_1_0_0_BETA("1.0.0", ProtocolType.BETA, ProtocolSide.ALL, List.of(Protocol.OAC), PV_1_0_0_CLASSIC); /** * The version string of the protocol version. */ @Getter private final String version; /** * The type of the protocol version. */ @Getter private final ProtocolType protocolType; /** * The side(s) the protocol version is intended for. */ @Getter private final ProtocolSide protocolSide; /** * List of protocol versions that are compatible with this version. */ @Getter private final List compatibleVersions; /** * List of supported protocols. */ @Getter private final List supportedProtocols; /** * Constructor for ProtocolVersion enum. * * @param version The version string. * @param protocolType The type of the protocol. * @param protocolSide The side(s) the protocol is intended for. * @param supportedProtocols List of supported protocols. * @param compatibleVersions Varargs of compatible protocol versions. */ ProtocolVersion(String version, ProtocolType protocolType, ProtocolSide protocolSide, List supportedProtocols, ProtocolVersion... compatibleVersions) { this.version = version; this.protocolType = protocolType; this.protocolSide = protocolSide; this.compatibleVersions = new ArrayList<>(Arrays.stream(compatibleVersions).toList()); if (!this.compatibleVersions.contains(this)) this.compatibleVersions.add(this); this.supportedProtocols = supportedProtocols; } /** * Returns a string representation of the protocol version, including its version, type, side, supported protocols, and compatible versions. * @return a string representation of the protocol version. */ @Override public final String toString() { StringBuilder compatible = new StringBuilder("["); StringBuilder supported = new StringBuilder("["); for (ProtocolVersion compatibleVersion : compatibleVersions) compatible.append(compatibleVersion.buildName()); for (Protocol supportedProtocol : supportedProtocols) supported.append(supportedProtocol.toString()); compatible.append("]"); supported.append("]"); return "{version=" + version + ";type=" + protocolType.toString() + ";side=" + protocolSide.toString() + ";supportedProtocols=" + supported + ";compatibleVersions=" + compatible + "}"; } /** * Builds a name for the protocol version combining its version and type. * @return a string representing the name of the protocol version. */ public final String buildName() { return version + "-" + protocolType.toString(); } /** * Enum representing different protocols. */ public enum Protocol implements Serializable { HTTP, HTTPS, OAC; /** * Returns the name of the protocol in uppercase. * @return the name of the protocol in uppercase. */ @Override public final String toString() { return name().toUpperCase(); } } /** * Enum representing different types of protocol versions. */ public enum ProtocolType implements Serializable { /** * Classic Protocol Type, see old OAC-Project: *_old */ CLASSIC, /** * Beta Protocol Type, may be unstable and subject to change. */ BETA, /** * Stable Protocol Type, recommended for production use. */ STABLE; /** * Returns the name of the protocol in uppercase. * @return the name of the protocol in uppercase. */ @Override public final String toString() { return name().toUpperCase(); } } /** * Enum representing different sides where the protocol version can be used. */ public enum ProtocolSide implements Serializable { /** * Client Side only */ CLIENT, // Protocol version can only used on Client /** * DNS Server Side only */ DNS, /** * Web Server Side only */ WEB, /** * Both DNS and Web Server Side */ WEB_DNS, /** * Both Client and DNS Server Side */ CLIENT_DNS, /** * Both Client and Web Server Side */ CLIENT_WEB, /** * All Sides */ ALL ; /** * Returns the name of the protocol in uppercase. * @return the name of the protocol in uppercase. */ @Override public final String toString() { return name().toUpperCase(); } } }