PageRenderTime 23ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/model/StringParameterDefinition.java

http://github.com/hudson/hudson
Java | 102 lines | 62 code | 14 blank | 26 comment | 1 complexity | 40f3534d2c729e0e7fb68417900568f8 MD5 | raw file
  1/*
  2 * The MIT License
  3 * 
  4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Luca Domenico Milanesio, Seiji Sogabe, Tom Huybrechts
  5 * 
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 * 
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 * 
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.model;
 25
 26import hudson.Extension;
 27import net.sf.json.JSONObject;
 28import org.apache.commons.lang3.builder.EqualsBuilder;
 29import org.apache.commons.lang3.builder.HashCodeBuilder;
 30import org.kohsuke.stapler.DataBoundConstructor;
 31import org.kohsuke.stapler.StaplerRequest;
 32
 33/**
 34 * Parameter whose value is a string value.
 35 */
 36public class StringParameterDefinition extends SimpleParameterDefinition {
 37
 38    private String defaultValue;
 39
 40    @DataBoundConstructor
 41    public StringParameterDefinition(String name, String defaultValue, String description) {
 42        super(name, description);
 43        this.defaultValue = defaultValue;
 44    }
 45
 46    public StringParameterDefinition(String name, String defaultValue) {
 47        this(name, defaultValue, null);
 48    }
 49
 50    public String getDefaultValue() {
 51        return defaultValue;
 52    }
 53
 54    public void setDefaultValue(String defaultValue) {
 55        this.defaultValue = defaultValue;
 56    }
 57    
 58    @Override
 59    public StringParameterValue getDefaultParameterValue() {
 60        StringParameterValue v = new StringParameterValue(getName(), defaultValue, getDescription());
 61        return v;
 62    }
 63
 64    @Extension
 65    public static class DescriptorImpl extends ParameterDescriptor {
 66        @Override
 67        public String getDisplayName() {
 68            return Messages.StringParameterDefinition_DisplayName();
 69        }
 70
 71        @Override
 72        public String getHelpFile() {
 73            return "/help/parameter/string.html";
 74        }
 75    }
 76
 77    @Override
 78    public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
 79        StringParameterValue value = req.bindJSON(StringParameterValue.class, jo);
 80        value.setDescription(getDescription());
 81        return value;
 82    }
 83
 84    public ParameterValue createValue(String value) {
 85        return new StringParameterValue(getName(), value, getDescription());
 86    }
 87
 88    @Override
 89    public boolean equals(Object o) {
 90        return super.equals(o) && new EqualsBuilder()
 91            .append(getDefaultValue(), ((StringParameterDefinition) o).getDefaultValue())
 92            .isEquals();
 93    }
 94
 95    @Override
 96    public int hashCode() {
 97        return new HashCodeBuilder()
 98            .appendSuper(super.hashCode())
 99            .append(getDefaultValue())
100            .toHashCode();
101    }
102}