001package com.github.theholywaffle.teamspeak3.commands; 002 003/* 004 * #%L 005 * TeamSpeak 3 Java API 006 * %% 007 * Copyright (C) 2014 Bert De Geyter 008 * %% 009 * Permission is hereby granted, free of charge, to any person obtaining a copy 010 * of this software and associated documentation files (the "Software"), to deal 011 * in the Software without restriction, including without limitation the rights 012 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 013 * copies of the Software, and to permit persons to whom the Software is 014 * furnished to do so, subject to the following conditions: 015 * 016 * The above copyright notice and this permission notice shall be included in 017 * all copies or substantial portions of the Software. 018 * 019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 022 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 024 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 025 * THE SOFTWARE. 026 * #L% 027 */ 028 029import com.github.theholywaffle.teamspeak3.api.wrapper.QueryError; 030import com.github.theholywaffle.teamspeak3.api.wrapper.Wrapper; 031import com.github.theholywaffle.teamspeak3.commands.parameter.Parameter; 032import com.github.theholywaffle.teamspeak3.commands.response.DefaultArrayResponse; 033 034import java.util.Collections; 035import java.util.LinkedList; 036import java.util.List; 037 038public class Command { 039 040 private final String command; 041 private final List<Parameter> params = new LinkedList<>(); 042 043 private boolean sent = false; 044 private boolean answered = false; 045 private DefaultArrayResponse response; 046 private QueryError error; 047 private String raw; 048 049 protected Command(String command) { 050 this.command = command; 051 } 052 053 protected void add(Parameter p) { 054 params.add(p); 055 } 056 057 public void feed(String str) { 058 raw = str; 059 if (response == null) { 060 response = new DefaultArrayResponse(str); 061 } 062 } 063 064 public void feedError(String err) { 065 if (error == null) { 066 final DefaultArrayResponse errorResponse = new DefaultArrayResponse(err); 067 error = new QueryError(errorResponse.getFirstResponse().getMap()); 068 } 069 } 070 071 public QueryError getError() { 072 return error; 073 } 074 075 public String getName() { 076 return command; 077 } 078 079 public Wrapper getFirstResponse() { 080 if (response == null || response.getArray().isEmpty()) { 081 return new Wrapper(Collections.<String, String>emptyMap()); 082 } 083 084 return response.getFirstResponse(); 085 } 086 087 public List<Wrapper> getResponse() { 088 if (response == null) return Collections.emptyList(); 089 return response.getArray(); 090 } 091 092 public boolean isAnswered() { 093 return answered; 094 } 095 096 public boolean isSent() { 097 return sent; 098 } 099 100 public void setAnswered() { 101 answered = true; 102 } 103 104 public void setSent() { 105 sent = true; 106 } 107 108 public Command reset() { 109 sent = false; 110 answered = false; 111 response = null; 112 error = null; 113 raw = null; 114 return this; 115 } 116 117 public String toString() { 118 StringBuilder builder = new StringBuilder(command); 119 for (final Parameter p : params) { 120 builder.append(" ").append(p); 121 } 122 return builder.toString(); 123 } 124 125 public String getRaw() { 126 return raw; 127 } 128 129}