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 { /** * For classic OAC-Project => "classic"-branch */ @Deprecated(forRemoval = false, since = "1.0.1-BETA.0.1") PV_1_0_0_CLASSIC("1.0.0", ProtocolType.CLASSIC, ProtocolSide.WEB_INS, List.of(Protocol.HTTP)), /** * Version {@link ProtocolVersion#PV_1_0_0_BETA} has many bugs and does not work as expected (occurred by incompleted packets). * Use {@link ProtocolVersion#PV_1_0_1_BETA} or newer. */ @Deprecated(forRemoval = false, since = "1.0.1-BETA.0.1") PV_1_0_0_BETA("1.0.0", ProtocolType.BETA, ProtocolSide.ALL, List.of(Protocol.OAC)), PV_1_0_1_BETA("1.0.1", ProtocolType.BETA, ProtocolSide.ALL, List.of(Protocol.OAC), PV_1_0_0_BETA), ; /** * 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, /** * INS Server Side only */ INS, /** * Web Server Side only */ WEB, /** * Both INS and Web Server Side */ WEB_INS, /** * Both Client and INS Server Side */ CLIENT_INS, /** * 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(); } } }