Introduce AbstractStats

This commit is contained in:
Florian Schmaus 2020-05-16 14:15:50 +02:00
parent b79ee8f6a9
commit dfdd0acf91
4 changed files with 134 additions and 13 deletions

View File

@ -65,6 +65,7 @@ import org.jivesoftware.smack.fsm.StateDescriptorGraph.GraphVertex;
import org.jivesoftware.smack.fsm.StateMachineException;
import org.jivesoftware.smack.fsm.StateTransitionResult;
import org.jivesoftware.smack.fsm.StateTransitionResult.AttemptResult;
import org.jivesoftware.smack.internal.AbstractStats;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Nonza;
@ -78,6 +79,7 @@ import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.sasl.SASLErrorException;
import org.jivesoftware.smack.sasl.SASLMechanism;
import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown;
import org.jivesoftware.smack.util.ExtendedAppendable;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
@ -1069,7 +1071,7 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
return new Stats(transportsStats, filterStats);
}
public static final class Stats {
public static final class Stats extends AbstractStats {
public final Map<Class<? extends ModularXmppClientToServerConnectionModuleDescriptor>, XmppClientToServerTransport.Stats> transportsStats;
public final Map<String, Object> filtersStats;
@ -1079,7 +1081,8 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
this.filtersStats = Collections.unmodifiableMap(filtersStats);
}
public void appendStatsTo(Appendable appendable) throws IOException {
@Override
public void appendStatsTo(ExtendedAppendable appendable) throws IOException {
StringUtils.appendHeading(appendable, "Connection stats", '#').append('\n');
for (Map.Entry<Class<? extends ModularXmppClientToServerConnectionModuleDescriptor>, XmppClientToServerTransport.Stats> entry : transportsStats.entrySet()) {
@ -1099,16 +1102,5 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
try {
appendStatsTo(sb);
} catch (IOException e) {
// Should never happen.
throw new AssertionError(e);
}
return sb.toString();
}
}
}

View File

@ -0,0 +1,52 @@
/**
*
* Copyright 2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.internal;
import java.io.IOException;
import org.jivesoftware.smack.util.ExtendedAppendable;
public abstract class AbstractStats {
public final void appendStatsTo(Appendable appendable) throws IOException {
appendStatsTo(new ExtendedAppendable(appendable));
}
public abstract void appendStatsTo(ExtendedAppendable appendable) throws IOException;
private transient String toStringCache;
@Override
public final String toString() {
if (toStringCache != null) {
return toStringCache;
}
StringBuilder sb = new StringBuilder();
try {
appendStatsTo(sb);
} catch (IOException e) {
// Should never happen.
throw new AssertionError(e);
}
toStringCache = sb.toString();
return toStringCache;
}
}

View File

@ -0,0 +1,21 @@
/**
*
* Copyright 2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Smack internal classes and interfaces.
*/
package org.jivesoftware.smack.internal;

View File

@ -0,0 +1,56 @@
/**
*
* Copyright 2020 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
import java.io.IOException;
public class ExtendedAppendable implements Appendable {
private final Appendable appendable;
public ExtendedAppendable(Appendable appendable) {
this.appendable = appendable;
}
@Override
public ExtendedAppendable append(CharSequence csq) throws IOException {
appendable.append(csq);
return this;
}
@Override
public ExtendedAppendable append(CharSequence csq, int start, int end) throws IOException {
appendable.append(csq, start, end);
return this;
}
@Override
public ExtendedAppendable append(char c) throws IOException {
appendable.append(c);
return this;
}
public ExtendedAppendable append(boolean b) throws IOException {
appendable.append(String.valueOf(b));
return this;
}
public ExtendedAppendable append(int i) throws IOException {
appendable.append(String.valueOf(i));
return this;
}
}