001/**
002 *
003 * Copyright 2020 Florian Schmaus.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.xdata;
018
019import java.util.List;
020
021import org.jivesoftware.smack.util.StringUtils;
022
023public class TextMultiFormField extends AbstractMultiFormField {
024
025    protected TextMultiFormField(Builder builder) {
026        super(builder);
027    }
028
029    public void addValuesWithNewlines(StringBuilder sb) {
030        for (CharSequence value : getValues()) {
031            sb.append(value);
032        }
033    }
034
035    public StringBuilder getValueswithNewlines() {
036        StringBuilder sb = new StringBuilder();
037        addValuesWithNewlines(sb);
038        return sb;
039    }
040
041    public Builder asBuilder() {
042        return new Builder(this);
043    }
044
045    public static final class Builder extends AbstractMultiFormField.Builder<TextMultiFormField, TextMultiFormField.Builder> {
046
047        private Builder(TextMultiFormField textMultiFormField) {
048            super(textMultiFormField);
049        }
050
051        Builder(String fieldName) {
052            super(fieldName, FormField.Type.text_multi);
053        }
054
055        @Override
056        public Builder addValue(CharSequence valueCharSequence) {
057            String value = valueCharSequence.toString();
058            List<String> lines = StringUtils.splitLinesPortable(value);
059            return addValues(lines);
060        }
061
062        @Override
063        public TextMultiFormField build() {
064            return new TextMultiFormField(this);
065        }
066
067        @Override
068        public Builder getThis() {
069            return this;
070        }
071    }
072
073}