001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot;
018
019import java.util.Arrays;
020import java.util.Collections;
021import java.util.HashSet;
022import java.util.List;
023import java.util.Set;
024
025import org.springframework.core.env.SimpleCommandLinePropertySource;
026import org.springframework.util.Assert;
027
028/**
029 * Default implementation of {@link ApplicationArguments}.
030 *
031 * @author Phillip Webb
032 * @since 1.4.1
033 */
034public class DefaultApplicationArguments implements ApplicationArguments {
035
036        private final Source source;
037
038        private final String[] args;
039
040        public DefaultApplicationArguments(String[] args) {
041                Assert.notNull(args, "Args must not be null");
042                this.source = new Source(args);
043                this.args = args;
044        }
045
046        @Override
047        public String[] getSourceArgs() {
048                return this.args;
049        }
050
051        @Override
052        public Set<String> getOptionNames() {
053                String[] names = this.source.getPropertyNames();
054                return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(names)));
055        }
056
057        @Override
058        public boolean containsOption(String name) {
059                return this.source.containsProperty(name);
060        }
061
062        @Override
063        public List<String> getOptionValues(String name) {
064                List<String> values = this.source.getOptionValues(name);
065                return (values != null) ? Collections.unmodifiableList(values) : null;
066        }
067
068        @Override
069        public List<String> getNonOptionArgs() {
070                return this.source.getNonOptionArgs();
071        }
072
073        private static class Source extends SimpleCommandLinePropertySource {
074
075                Source(String[] args) {
076                        super(args);
077                }
078
079                @Override
080                public List<String> getNonOptionArgs() {
081                        return super.getNonOptionArgs();
082                }
083
084                @Override
085                public List<String> getOptionValues(String name) {
086                        return super.getOptionValues(name);
087                }
088
089        }
090
091}