001/** 002 * 003 * Copyright 2019-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.disco.packet; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.List; 022 023import org.jivesoftware.smack.XMPPConnection; 024import org.jivesoftware.smack.packet.IQ; 025import org.jivesoftware.smack.packet.IqBuilder; 026import org.jivesoftware.smack.packet.IqData; 027 028import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Feature; 029import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity; 030 031public class DiscoverInfoBuilder extends IqBuilder<DiscoverInfoBuilder, DiscoverInfo> 032 implements DiscoverInfoView { 033 034 private final List<Feature> features = new ArrayList<>(); 035 private final List<Identity> identities = new ArrayList<>(); 036 037 private String node; 038 039 DiscoverInfoBuilder(IqData iqCommon) { 040 super(iqCommon); 041 } 042 043 DiscoverInfoBuilder(XMPPConnection connection) { 044 super(connection); 045 } 046 047 DiscoverInfoBuilder(String stanzaId) { 048 super(stanzaId); 049 } 050 051 public DiscoverInfoBuilder(DiscoverInfo discoverInfo, String stanzaId) { 052 super(discoverInfo, stanzaId); 053 features.addAll(discoverInfo.getFeatures()); 054 identities.addAll(discoverInfo.getIdentities()); 055 node = discoverInfo.getNode(); 056 } 057 058 @Override 059 public DiscoverInfoBuilder getThis() { 060 return this; 061 } 062 063 public DiscoverInfoBuilder addFeatures(Collection<String> features) { 064 for (String feature : features) { 065 addFeature(feature); 066 } 067 return getThis(); 068 } 069 070 public DiscoverInfoBuilder addFeature(String feature) { 071 return addFeature(new Feature(feature)); 072 } 073 074 public DiscoverInfoBuilder addFeature(Feature feature) { 075 features.add(feature); 076 return getThis(); 077 } 078 079 public DiscoverInfoBuilder addIdentities(Collection<Identity> identities) { 080 this.identities.addAll(identities); 081 return getThis(); 082 } 083 084 public DiscoverInfoBuilder addIdentity(Identity identity) { 085 identities.add(identity); 086 return getThis(); 087 } 088 089 public DiscoverInfoBuilder setNode(String node) { 090 this.node = node; 091 return getThis(); 092 } 093 094 @Override 095 public DiscoverInfo build() { 096 return new DiscoverInfo(this, true); 097 } 098 099 public DiscoverInfo buildWithoutValidiation() { 100 return new DiscoverInfo(this, false); 101 } 102 103 @Override 104 public List<Feature> getFeatures() { 105 return features; 106 } 107 108 @Override 109 public List<Identity> getIdentities() { 110 return identities; 111 } 112 113 @Override 114 public String getNode() { 115 return node; 116 } 117 118 public static DiscoverInfoBuilder buildResponseFor(DiscoverInfo request, IQ.ResponseType responseType) { 119 DiscoverInfoBuilder builder = new DiscoverInfoBuilder(createResponse(request, responseType)); 120 builder.setNode(request.getNode()); 121 return builder; 122 } 123}