/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. */
  24. package hudson.model;
  25. import hudson.Extension;
  26. import net.sf.json.JSONObject;
  27. import org.apache.commons.lang3.builder.EqualsBuilder;
  28. import org.apache.commons.lang3.builder.HashCodeBuilder;
  29. import org.kohsuke.stapler.DataBoundConstructor;
  30. import org.kohsuke.stapler.StaplerRequest;
  31. /**
  32. * Parameter whose value is a string value.
  33. */
  34. public class StringParameterDefinition extends SimpleParameterDefinition {
  35. private String defaultValue;
  36. @DataBoundConstructor
  37. public StringParameterDefinition(String name, String defaultValue, String description) {
  38. super(name, description);
  39. this.defaultValue = defaultValue;
  40. }
  41. public StringParameterDefinition(String name, String defaultValue) {
  42. this(name, defaultValue, null);
  43. }
  44. public String getDefaultValue() {
  45. return defaultValue;
  46. }
  47. public void setDefaultValue(String defaultValue) {
  48. this.defaultValue = defaultValue;
  49. }
  50. @Override
  51. public StringParameterValue getDefaultParameterValue() {
  52. StringParameterValue v = new StringParameterValue(getName(), defaultValue, getDescription());
  53. return v;
  54. }
  55. @Extension
  56. public static class DescriptorImpl extends ParameterDescriptor {
  57. @Override
  58. public String getDisplayName() {
  59. return Messages.StringParameterDefinition_DisplayName();
  60. }
  61. @Override
  62. public String getHelpFile() {
  63. return "/help/parameter/string.html";
  64. }
  65. }
  66. @Override
  67. public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
  68. StringParameterValue value = req.bindJSON(StringParameterValue.class, jo);
  69. value.setDescription(getDescription());
  70. return value;
  71. }
  72. public ParameterValue createValue(String value) {
  73. return new StringParameterValue(getName(), value, getDescription());
  74. }
  75. @Override
  76. public boolean equals(Object o) {
  77. return super.equals(o) && new EqualsBuilder()
  78. .append(getDefaultValue(), ((StringParameterDefinition) o).getDefaultValue())
  79. .isEquals();
  80. }
  81. @Override
  82. public int hashCode() {
  83. return new HashCodeBuilder()
  84. .appendSuper(super.hashCode())
  85. .append(getDefaultValue())
  86. .toHashCode();
  87. }
  88. }