001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2024 Smile CDR, Inc.
006 * %%
007 * All rights reserved.
008 * #L%
009 */
010package ca.cdr.api.security.permission;
011
012import jakarta.annotation.Nonnull;
013import org.apache.commons.lang3.StringUtils;
014
015import java.util.Optional;
016
017/**
018 * Our no-parameter parameter value.
019 * Accepts null and the empty string.
020 */
021public final class NoArgument extends PermissionArgumentValue {
022
023        public static final NoArgument INSTANCE = new NoArgument();
024
025        /**
026         * private constructor to force singleton instance
027         */
028        private NoArgument() {}
029
030        @Override
031        public String toString() {
032                // Normally the formatted version, but for NoArgument,
033                // we show some noise instead of the empty string for better debugging
034                return "[NoArgument.class]";
035        }
036
037        @Override
038        public @Nonnull String getStringValue() {
039                return "";
040        }
041
042        static final class NoArgumentFormat implements PermissionArgumentFormat<NoArgument> {
043
044                @Override
045                public @Nonnull Optional<NoArgument> parse(String theArgumentString, @Nonnull IdBuilder theIdBuilder) {
046                        if (StringUtils.isEmpty(theArgumentString)) {
047                                return Optional.of(NoArgument.INSTANCE);
048                        }
049                        return Optional.empty();
050                }
051
052                @Override
053                public Class<NoArgument> getType() {
054                        return NoArgument.class;
055                }
056
057                @Override
058                public @Nonnull String format(@Nonnull NoArgument theArgument) {
059                        return theArgument.getStringValue();
060                }
061        }
062}