001/** 002 * 003 * Copyright 2019 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.smack.packet; 018 019import org.jivesoftware.smack.packet.Presence.Mode; 020import org.jivesoftware.smack.packet.id.StanzaIdSource; 021import org.jivesoftware.smack.util.Objects; 022import org.jivesoftware.smack.util.ToStringUtil; 023 024public final class PresenceBuilder extends MessageOrPresenceBuilder<Presence, PresenceBuilder> implements PresenceView { 025 static final PresenceBuilder EMPTY = new PresenceBuilder(() -> { 026 return null; 027 }); 028 029 Presence.Type type = Presence.Type.available; 030 031 String status; 032 033 Byte priority; 034 035 Presence.Mode mode; 036 037 PresenceBuilder(Presence presence, String stanzaId) { 038 super(presence, stanzaId); 039 copyFromPresence(presence); 040 } 041 042 PresenceBuilder(Presence presence, StanzaIdSource stanzaIdSource) { 043 super(presence, stanzaIdSource); 044 copyFromPresence(presence); 045 } 046 047 PresenceBuilder(StanzaIdSource stanzaIdSource) { 048 super(stanzaIdSource); 049 } 050 051 PresenceBuilder(String stanzaId) { 052 super(stanzaId); 053 } 054 055 private void copyFromPresence(Presence presence) { 056 type = presence.getType(); 057 status = presence.getStatus(); 058 priority = presence.getPriorityByte(); 059 mode = presence.getMode(); 060 } 061 062 @Override 063 protected void addStanzaSpecificAttributes(ToStringUtil.Builder builder) { 064 builder.addValue("type", type) 065 .addValue("mode", mode) 066 .addValue("priority", priority) 067 .addValue("status", status) 068 ; 069 } 070 071 public PresenceBuilder ofType(Presence.Type type) { 072 this.type = Objects.requireNonNull(type, "Type cannot be null"); 073 return getThis(); 074 } 075 076 public PresenceBuilder setStatus(String status) { 077 this.status = status; 078 return getThis(); 079 } 080 081 public PresenceBuilder setPriority(int priority) { 082 if (priority < -128 || priority > 127) { 083 throw new IllegalArgumentException("Priority value " + priority + 084 " is not valid. Valid range is -128 through 127."); 085 } 086 Byte priorityByte = (byte) priority; 087 return setPriority(priorityByte); 088 } 089 090 public PresenceBuilder setPriority(Byte priority) { 091 this.priority = priority; 092 return getThis(); 093 } 094 095 public PresenceBuilder setMode(Presence.Mode mode) { 096 this.mode = mode; 097 return getThis(); 098 } 099 100 @Override 101 public PresenceBuilder getThis() { 102 return this; 103 } 104 105 @Override 106 public Presence build() { 107 return new Presence(this); 108 } 109 110 @Override 111 public Presence.Type getType() { 112 return type; 113 } 114 115 @Override 116 public String getStatus() { 117 return status; 118 } 119 120 @Override 121 public int getPriority() { 122 return getPriorityByte(); 123 } 124 125 @Override 126 public byte getPriorityByte() { 127 if (priority == null) { 128 return 0; 129 } 130 return priority; 131 } 132 133 @Override 134 public Presence.Mode getMode() { 135 if (mode == null) { 136 return Mode.available; 137 } 138 return mode; 139 } 140 141}