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;
013
014import java.util.Optional;
015
016import static org.apache.commons.lang3.StringUtils.isNotBlank;
017
018/**
019 * An operation name.
020 * No validation.  Accepts anything.
021 */
022public final class Operation extends PermissionArgumentValue implements PermissionArgumentFormat.IOperationRestriction {
023        // final for optimizer
024
025        private final String myOperationName;
026
027        Operation(String theOperationName) {
028                myOperationName = theOperationName;
029        }
030
031        @Override
032        public String getOperationName() {
033                return myOperationName;
034        }
035
036        @Override
037        @Nonnull
038        String getStringValue() {
039                return myOperationName;
040        }
041
042        static final class Format implements PermissionArgumentFormat<Operation> {
043
044                @Override
045                public Class<Operation> getType() {
046                        return Operation.class;
047                }
048
049                @Override
050                public @Nonnull Optional<Operation> parse(String theArgumentString, @Nonnull IdBuilder theIdBuilder) {
051                        if (isNotBlank(theArgumentString)) {
052                                return Optional.of(new Operation(theArgumentString));
053                        }
054
055                        return Optional.empty();
056                }
057
058                @Override
059                public @Nonnull String format(@Nonnull Operation theArgument) {
060                        return theArgument.getStringValue();
061                }
062        }
063}