001/** 002 * 003 * Copyright 2020-2021 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.ArrayList; 020import java.util.Collection; 021import java.util.List; 022 023import org.jivesoftware.smack.util.CollectionUtil; 024 025import org.jxmpp.jid.Jid; 026import org.jxmpp.jid.impl.JidCreate; 027import org.jxmpp.stringprep.XmppStringprepException; 028 029public final class JidMultiFormField extends FormField { 030 031 private final List<Jid> values; 032 033 private final List<Value> rawValues; 034 035 JidMultiFormField(Builder builder) { 036 super(builder); 037 values = CollectionUtil.cloneAndSeal(builder.values); 038 rawValues = CollectionUtil.cloneAndSeal(builder.rawValues); 039 } 040 041 @Override 042 public List<Jid> getValues() { 043 return values; 044 } 045 046 @Override 047 public List<Value> getRawValues() { 048 return rawValues; 049 } 050 051 public Builder asBuilder() { 052 return new Builder(this); 053 } 054 055 public static final class Builder extends FormField.Builder<JidMultiFormField, JidMultiFormField.Builder> { 056 private List<Jid> values; 057 058 private List<Value> rawValues; 059 060 private Builder(JidMultiFormField jidMultiFormField) { 061 super(jidMultiFormField); 062 values = CollectionUtil.newListWith(jidMultiFormField.getValues()); 063 } 064 065 Builder(String fieldName) { 066 super(fieldName, FormField.Type.jid_multi); 067 } 068 069 private void ensureValuesAreInitialized() { 070 if (values == null) { 071 values = new ArrayList<>(); 072 rawValues = new ArrayList<>(); 073 } 074 } 075 076 @Override 077 protected void resetInternal() { 078 values = null; 079 rawValues = null; 080 } 081 082 public Builder addValue(Jid jid) { 083 Value value = new Value(jid); 084 085 ensureValuesAreInitialized(); 086 values.add(jid); 087 rawValues.add(value); 088 089 return getThis(); 090 } 091 092 public Builder addValue(Value value) throws XmppStringprepException { 093 Jid jid = JidCreate.from(value.getValue()); 094 095 ensureValuesAreInitialized(); 096 values.add(jid); 097 rawValues.add(value); 098 099 return this; 100 } 101 102 public Builder addValues(Collection<? extends Jid> jids) { 103 for (Jid jid : jids) { 104 addValue(jid); 105 } 106 return this; 107 } 108 109 @Override 110 public JidMultiFormField build() { 111 return new JidMultiFormField(this); 112 } 113 114 @Override 115 public Builder getThis() { 116 return this; 117 } 118 } 119 120 121}