From dfdd0acf913e3c9aefa42e6247adccbe12061766 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 16 May 2020 14:15:50 +0200 Subject: [PATCH] Introduce AbstractStats --- .../ModularXmppClientToServerConnection.java | 18 ++---- .../smack/internal/AbstractStats.java | 52 +++++++++++++++++ .../smack/internal/package-info.java | 21 +++++++ .../smack/util/ExtendedAppendable.java | 56 +++++++++++++++++++ 4 files changed, 134 insertions(+), 13 deletions(-) create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/internal/AbstractStats.java create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/internal/package-info.java create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/util/ExtendedAppendable.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/c2s/ModularXmppClientToServerConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/c2s/ModularXmppClientToServerConnection.java index 9694b47b8..eff02dedb 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/c2s/ModularXmppClientToServerConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/c2s/ModularXmppClientToServerConnection.java @@ -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, XmppClientToServerTransport.Stats> transportsStats; public final Map 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, 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(); - } } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/internal/AbstractStats.java b/smack-core/src/main/java/org/jivesoftware/smack/internal/AbstractStats.java new file mode 100644 index 000000000..1cddd964c --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/internal/AbstractStats.java @@ -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; + } + +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/internal/package-info.java b/smack-core/src/main/java/org/jivesoftware/smack/internal/package-info.java new file mode 100644 index 000000000..6311fbe06 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/internal/package-info.java @@ -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; diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/ExtendedAppendable.java b/smack-core/src/main/java/org/jivesoftware/smack/util/ExtendedAppendable.java new file mode 100644 index 000000000..4cf97622c --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/ExtendedAppendable.java @@ -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; + } +}