1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-24 11:38:00 +01:00

Improve JingleS5BTransportInfo by introducing abstract class

Introduce a abstract class for shared functionality. And remove static
lazy getters.
This commit is contained in:
Florian Schmaus 2017-07-30 19:06:13 +02:00
parent b8ee8d808f
commit ed9eae4793
4 changed files with 62 additions and 100 deletions

View file

@ -133,19 +133,19 @@ public class JingleS5BTransport extends JingleContentTransport {
}
public Builder setCandidateUsed(String candidateId) {
return setTransportInfo(JingleS5BTransportInfo.CandidateUsed(candidateId));
return setTransportInfo(new JingleS5BTransportInfo.CandidateUsed(candidateId));
}
public Builder setCandidateActivated(String candidateId) {
return setTransportInfo(JingleS5BTransportInfo.CandidateActivated(candidateId));
return setTransportInfo(new JingleS5BTransportInfo.CandidateActivated(candidateId));
}
public Builder setCandidateError() {
return setTransportInfo(JingleS5BTransportInfo.CandidateError());
return setTransportInfo(JingleS5BTransportInfo.CandidateError.INSTANCE);
}
public Builder setProxyError() {
return setTransportInfo(JingleS5BTransportInfo.ProxyError());
return setTransportInfo(JingleS5BTransportInfo.ProxyError.INSTANCE);
}
public JingleS5BTransport build() {

View file

@ -24,116 +24,76 @@ import org.jivesoftware.smackx.jingle.element.JingleContentTransportInfo;
*/
public abstract class JingleS5BTransportInfo extends JingleContentTransportInfo {
private static CandidateError CEI;
private static ProxyError PEI;
public static CandidateUsed CandidateUsed(String candidateId) {
return new CandidateUsed(candidateId);
}
public static CandidateActivated CandidateActivated(String candidateId) {
return new CandidateActivated(candidateId);
}
public static CandidateError CandidateError() {
if (CEI == null) {
CEI = new CandidateError();
}
return CEI;
}
public static ProxyError ProxyError() {
if (PEI == null) {
PEI = new ProxyError();
}
return PEI;
}
public static final class CandidateActivated extends JingleS5BTransportInfo {
public static final String ELEMENT = "candidate-activated";
public static abstract class JingleS5BCandidateTransportInfo extends JingleS5BTransportInfo {
public static final String ATTR_CID = "cid";
private final String candidateId;
protected JingleS5BCandidateTransportInfo(String candidateId) {
this.candidateId = candidateId;
}
public final String getCandidateId() {
return candidateId;
}
@Override
public final XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(this);
xml.attribute(ATTR_CID, getCandidateId());
xml.closeEmptyElement();
return xml;
}
@Override
public final boolean equals(Object other) {
if (!(other instanceof JingleS5BCandidateTransportInfo)) {
return false;
}
JingleS5BCandidateTransportInfo otherCandidateTransportInfo = (JingleS5BCandidateTransportInfo) other;
return toXML().equals(otherCandidateTransportInfo.toXML());
}
@Override
public final int hashCode() {
return getCandidateId().hashCode();
}
}
public static final class CandidateActivated extends JingleS5BCandidateTransportInfo {
public static final String ELEMENT = "candidate-activated";
public CandidateActivated(String candidateId) {
this.candidateId = candidateId;
}
public String getCandidateId() {
return candidateId;
super(candidateId);
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public CharSequence toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(this);
xml.attribute(ATTR_CID, candidateId);
xml.closeEmptyElement();
return xml;
}
@Override
public boolean equals(Object other) {
return other instanceof CandidateActivated &&
((CandidateActivated) other).getCandidateId().equals(candidateId);
}
@Override
public int hashCode() {
return toXML().toString().hashCode();
}
}
public static final class CandidateUsed extends JingleS5BTransportInfo {
public static final class CandidateUsed extends JingleS5BCandidateTransportInfo {
public static final String ELEMENT = "candidate-used";
public static final String ATTR_CID = "cid";
private final String candidateId;
public CandidateUsed(String candidateId) {
this.candidateId = candidateId;
}
public String getCandidateId() {
return candidateId;
super(candidateId);
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public CharSequence toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(this);
xml.attribute(ATTR_CID, candidateId);
xml.closeEmptyElement();
return xml;
}
@Override
public boolean equals(Object other) {
return other instanceof CandidateUsed &&
((CandidateUsed) other).getCandidateId().equals(candidateId);
}
@Override
public int hashCode() {
return toXML().toString().hashCode();
}
}
public static final class CandidateError extends JingleS5BTransportInfo {
public static final CandidateError INSTANCE = new CandidateError();
public static final String ELEMENT = "candidate-error";
private CandidateError() {
}
@Override
@ -142,7 +102,7 @@ public abstract class JingleS5BTransportInfo extends JingleContentTransportInfo
}
@Override
public CharSequence toXML() {
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(this);
xml.closeEmptyElement();
@ -151,7 +111,7 @@ public abstract class JingleS5BTransportInfo extends JingleContentTransportInfo
@Override
public boolean equals(Object other) {
return other instanceof CandidateError;
return other == INSTANCE;
}
@Override
@ -161,10 +121,11 @@ public abstract class JingleS5BTransportInfo extends JingleContentTransportInfo
}
public static final class ProxyError extends JingleS5BTransportInfo {
public static final ProxyError INSTANCE = new ProxyError();
public static final String ELEMENT = "proxy-error";
private ProxyError() {
}
@Override
@ -182,7 +143,7 @@ public abstract class JingleS5BTransportInfo extends JingleContentTransportInfo
@Override
public boolean equals(Object other) {
return other instanceof ProxyError;
return other == INSTANCE;
}
@Override

View file

@ -32,6 +32,7 @@ import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider;
import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransport;
import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate;
import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportInfo;
import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportInfo.JingleS5BCandidateTransportInfo;
import org.xmlpull.v1.XmlPullParser;
@ -83,23 +84,23 @@ public class JingleS5BTransportProvider extends JingleContentTransportProvider<J
break;
case JingleS5BTransportInfo.CandidateActivated.ELEMENT:
builder.setTransportInfo(JingleS5BTransportInfo.CandidateActivated(
builder.setTransportInfo(new JingleS5BTransportInfo.CandidateActivated(
parser.getAttributeValue(null,
JingleS5BTransportInfo.CandidateActivated.ATTR_CID)));
JingleS5BCandidateTransportInfo.ATTR_CID)));
break;
case JingleS5BTransportInfo.CandidateUsed.ELEMENT:
builder.setTransportInfo(JingleS5BTransportInfo.CandidateUsed(
builder.setTransportInfo(new JingleS5BTransportInfo.CandidateUsed(
parser.getAttributeValue(null,
JingleS5BTransportInfo.CandidateUsed.ATTR_CID)));
JingleS5BCandidateTransportInfo.ATTR_CID)));
break;
case JingleS5BTransportInfo.CandidateError.ELEMENT:
builder.setTransportInfo(JingleS5BTransportInfo.CandidateError());
builder.setTransportInfo(JingleS5BTransportInfo.CandidateError.INSTANCE);
break;
case JingleS5BTransportInfo.ProxyError.ELEMENT:
builder.setTransportInfo(JingleS5BTransportInfo.ProxyError());
builder.setTransportInfo(JingleS5BTransportInfo.ProxyError.INSTANCE);
break;
}
}

View file

@ -127,7 +127,7 @@ public class JingleS5BTransportTest extends SmackTestSuite {
assertNull(candidateErrorTransport.getDestinationAddress());
assertNotNull(candidateErrorTransport.getInfo());
assertEquals("vj3hs98y", candidateErrorTransport.getStreamId());
assertEquals(JingleS5BTransportInfo.CandidateError(),
assertEquals(JingleS5BTransportInfo.CandidateError.INSTANCE,
candidateErrorTransport.getInfo());
assertEquals(candidateError, candidateErrorTransport.toXML().toString());
@ -141,7 +141,7 @@ public class JingleS5BTransportTest extends SmackTestSuite {
assertNotNull(proxyErrorTransport.getInfo());
assertNotNull(candidateErrorTransport.getInfo());
assertEquals("vj3hs98y", proxyErrorTransport.getStreamId());
assertEquals(JingleS5BTransportInfo.ProxyError(),
assertEquals(JingleS5BTransportInfo.ProxyError.INSTANCE,
proxyErrorTransport.getInfo());
assertEquals(proxyError, proxyErrorTransport.toXML().toString());
@ -152,7 +152,7 @@ public class JingleS5BTransportTest extends SmackTestSuite {
JingleS5BTransport candidateUsedTransport = new JingleS5BTransportProvider()
.parse(TestUtils.getParser(candidateUsed));
assertNotNull(candidateUsedTransport.getInfo());
assertEquals(JingleS5BTransportInfo.CandidateUsed("hr65dqyd"),
assertEquals(new JingleS5BTransportInfo.CandidateUsed("hr65dqyd"),
candidateUsedTransport.getInfo());
assertEquals("hr65dqyd",
((JingleS5BTransportInfo.CandidateUsed)
@ -168,7 +168,7 @@ public class JingleS5BTransportTest extends SmackTestSuite {
assertNotNull(candidateActivatedTransport.getInfo());
assertNotNull(candidateErrorTransport.getInfo());
assertEquals(JingleS5BTransportInfo.CandidateActivated("hr65dqyd"),
assertEquals(new JingleS5BTransportInfo.CandidateActivated("hr65dqyd"),
candidateActivatedTransport.getInfo());
assertEquals("hr65dqyd",
((JingleS5BTransportInfo.CandidateActivated)