TextMultiFormField.java

  1. /**
  2.  *
  3.  * Copyright 2020 Florian Schmaus.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.xdata;

  18. import java.util.List;

  19. import org.jivesoftware.smack.util.StringUtils;

  20. public class TextMultiFormField extends AbstractMultiFormField {

  21.     protected TextMultiFormField(Builder builder) {
  22.         super(builder);
  23.     }

  24.     public void addValuesWithNewlines(StringBuilder sb) {
  25.         for (CharSequence value : getValues()) {
  26.             sb.append(value);
  27.         }
  28.     }

  29.     public StringBuilder getValueswithNewlines() {
  30.         StringBuilder sb = new StringBuilder();
  31.         addValuesWithNewlines(sb);
  32.         return sb;
  33.     }

  34.     public Builder asBuilder() {
  35.         return new Builder(this);
  36.     }

  37.     public static final class Builder extends AbstractMultiFormField.Builder<TextMultiFormField, TextMultiFormField.Builder> {

  38.         private Builder(TextMultiFormField textMultiFormField) {
  39.             super(textMultiFormField);
  40.         }

  41.         Builder(String fieldName) {
  42.             super(fieldName, FormField.Type.text_multi);
  43.         }

  44.         @Override
  45.         public Builder addValue(CharSequence valueCharSequence) {
  46.             String value = valueCharSequence.toString();
  47.             List<String> lines = StringUtils.splitLinesPortable(value);
  48.             return addValues(lines);
  49.         }

  50.         @Override
  51.         public TextMultiFormField build() {
  52.             return new TextMultiFormField(this);
  53.         }

  54.         @Override
  55.         public Builder getThis() {
  56.             return this;
  57.         }
  58.     }

  59. }