Refactored ternary operators

This commit is contained in:
Patrick Gotthard 2013-10-04 11:23:02 +02:00
parent 2e87f7206d
commit 1c792aef1b
17 changed files with 201 additions and 42 deletions

View file

@ -174,7 +174,9 @@ public class Content implements Cloneable, Serializable {
* @param mode the content mode, <b>null</b> if none.
*/
public void setMode(String mode) {
mode = mode != null ? mode.toLowerCase() : null;
if (mode != null) {
mode = mode.toLowerCase();
}
if (mode == null || !MODES.contains(mode)) {
throw new IllegalArgumentException("Invalid mode [" + mode + "]");
}

View file

@ -270,7 +270,11 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @param issued the entry issued date, <b>null</b> if none.
*/
public void setIssued(final Date issued) {
published = issued == null ? null : new Date(issued.getTime());
if (issued == null) {
published = null;
} else {
published = new Date(issued.getTime());
}
}
/**
@ -316,7 +320,11 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @param modified the entry modified date, <b>null</b> if none.
*/
public void setModified(final Date modified) {
updated = modified == null ? null : new Date(modified.getTime());
if (modified == null) {
updated = null;
} else {
updated = new Date(modified.getTime());
}
}
/**
@ -408,7 +416,11 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @since Atom 1.0
*/
public void setPublished(final Date published) {
this.published = published == null ? null : new Date(published.getTime());
if (published == null) {
this.published = null;
} else {
this.published = new Date(published.getTime());
}
}
/**
@ -550,7 +562,11 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @since Atom 1.0
*/
public void setUpdated(final Date updated) {
this.updated = updated == null ? null : new Date(updated.getTime());
if (updated == null) {
this.updated = null;
} else {
this.updated = new Date(updated.getTime());
}
}
/**

View file

@ -105,7 +105,11 @@ public class CloneableBean implements Serializable, Cloneable {
*/
public CloneableBean(final Object obj, final Set ignoreProperties) {
this.obj = obj;
this.ignoreProperties = ignoreProperties != null ? ignoreProperties : Collections.EMPTY_SET;
if (ignoreProperties == null) {
this.ignoreProperties = Collections.emptySet();
} else {
this.ignoreProperties = ignoreProperties;
}
}
/**

View file

@ -116,7 +116,9 @@ public class CopyFromHelper {
if (value instanceof CopyFrom) {
final CopyFrom source = (CopyFrom) value;
CopyFrom target = createInstance(source.getInterface());
target = target == null ? (CopyFrom) value.getClass().newInstance() : target;
if (target == null) {
target = (CopyFrom) value.getClass().newInstance();
}
target.copyFrom(source);
value = target;
} else {
@ -140,7 +142,12 @@ public class CopyFromHelper {
private Object doCopyCollection(final Collection collection, final Class baseInterface) throws Exception {
// expecting SETs or LISTs only, going default implementation of them
final Collection newColl = collection instanceof Set ? (Collection) new HashSet() : (Collection) new ArrayList();
final Collection newColl;
if (collection instanceof Set) {
newColl = new HashSet();
} else {
newColl = new ArrayList();
}
final Iterator i = collection.iterator();
while (i.hasNext()) {
final Object element = doCopy(i.next(), baseInterface);

View file

@ -111,9 +111,14 @@ public class ToStringBean implements Serializable {
*/
@Override
public String toString() {
final Stack stack = (Stack) PREFIX_TL.get();
final String[] tsInfo = (String[]) (stack.isEmpty() ? null : stack.peek());
String prefix;
final Stack<String[]> stack = (Stack<String[]>) PREFIX_TL.get();
final String[] tsInfo;
if (stack.isEmpty()) {
tsInfo = null;
} else {
tsInfo = stack.peek();
}
final String prefix;
if (tsInfo == null) {
final String className = obj.getClass().getName();
prefix = className.substring(className.lastIndexOf(".") + 1);
@ -181,7 +186,12 @@ public class ToStringBean implements Serializable {
tsInfo[0] = ePrefix;
final Stack stack = (Stack) PREFIX_TL.get();
stack.push(tsInfo);
final String s = eValue != null ? eValue.toString() : "null";
final String s;
if (eValue != null) {
s = eValue.toString();
} else {
s = "null";
}
stack.pop();
if (tsInfo[1] == null) {
sb.append(ePrefix).append("=").append(s).append("\n");
@ -206,7 +216,12 @@ public class ToStringBean implements Serializable {
tsInfo[0] = cPrefix;
final Stack stack = (Stack) PREFIX_TL.get();
stack.push(tsInfo);
final String s = cValue != null ? cValue.toString() : "null";
final String s;
if (cValue != null) {
s = cValue.toString();
} else {
s = "null";
}
stack.pop();
if (tsInfo[1] == null) {
sb.append(cPrefix).append("=").append(s).append("\n");

View file

@ -349,7 +349,11 @@ public class Channel extends WireFeed {
*
*/
public void setPubDate(final Date pubDate) {
this.pubDate = pubDate == null ? null : new Date(pubDate.getTime());
if (pubDate == null) {
this.pubDate = null;
} else {
this.pubDate = new Date(pubDate.getTime());
}
}
/**
@ -376,7 +380,11 @@ public class Channel extends WireFeed {
*
*/
public void setLastBuildDate(final Date lastBuildDate) {
this.lastBuildDate = lastBuildDate == null ? null : new Date(lastBuildDate.getTime());
if (lastBuildDate == null) {
this.lastBuildDate = null;
} else {
this.lastBuildDate = new Date(lastBuildDate.getTime());
}
}
/**

View file

@ -445,7 +445,11 @@ public class Item implements Cloneable, Serializable, Extendable {
*
*/
public void setPubDate(final Date pubDate) {
this.pubDate = pubDate == null ? null : new Date(pubDate.getTime());
if (pubDate == null) {
this.pubDate = null;
} else {
this.pubDate = new Date(pubDate.getTime());
}
}
/**
@ -472,7 +476,11 @@ public class Item implements Cloneable, Serializable, Extendable {
*
*/
public void setExpirationDate(final Date expirationDate) {
this.expirationDate = expirationDate == null ? null : new Date(expirationDate.getTime());
if (expirationDate == null) {
this.expirationDate = null;
} else {
this.expirationDate = new Date(expirationDate.getTime());
}
}
/**

View file

@ -253,7 +253,12 @@ class SyndCategoryListFacade extends AbstractList<SyndCategory> {
@Override
public SyndCategory set(final int index, final SyndCategory obj) {
final SyndCategoryImpl sCat = (SyndCategoryImpl) obj;
DCSubject subject = sCat != null ? sCat.getSubject() : null;
DCSubject subject;
if (sCat != null) {
subject = sCat.getSubject();
} else {
subject = null;
}
subject = subjects.set(index, subject);
if (subject != null) {
return new SyndCategoryImpl(subject);
@ -273,7 +278,12 @@ class SyndCategoryListFacade extends AbstractList<SyndCategory> {
@Override
public void add(final int index, final SyndCategory obj) {
final SyndCategoryImpl sCat = (SyndCategoryImpl) obj;
final DCSubject subject = sCat != null ? sCat.getSubject() : null;
DCSubject subject;
if (sCat != null) {
subject = sCat.getSubject();
} else {
subject = null;
}
subjects.add(index, subject);
}

View file

@ -306,7 +306,12 @@ public class WireFeedInput {
* @return a new org.jdom2.input.SAXBuilder object
*/
protected SAXBuilder createSAXBuilder() {
final SAXBuilder saxBuilder = new SAXBuilder(validate ? XMLReaders.DTDVALIDATING : XMLReaders.NONVALIDATING);
SAXBuilder saxBuilder;
if (validate) {
saxBuilder = new SAXBuilder(XMLReaders.DTDVALIDATING);
} else {
saxBuilder = new SAXBuilder(XMLReaders.NONVALIDATING);
}
saxBuilder.setEntityResolver(RESOLVER);
//

View file

@ -132,7 +132,12 @@ public class WireFeedOutput {
public String outputString(final WireFeed feed, final boolean prettyPrint) throws IllegalArgumentException, FeedException {
final Document doc = outputJDom(feed);
final String encoding = feed.getEncoding();
final Format format = prettyPrint ? Format.getPrettyFormat() : Format.getCompactFormat();
Format format;
if (prettyPrint) {
format = Format.getPrettyFormat();
} else {
format = Format.getCompactFormat();
}
if (encoding != null) {
format.setEncoding(encoding);
}
@ -257,7 +262,12 @@ public class WireFeedOutput {
public void output(final WireFeed feed, final Writer writer, final boolean prettyPrint) throws IllegalArgumentException, IOException, FeedException {
final Document doc = outputJDom(feed);
final String encoding = feed.getEncoding();
final Format format = prettyPrint ? Format.getPrettyFormat() : Format.getCompactFormat();
Format format;
if (prettyPrint) {
format = Format.getPrettyFormat();
} else {
format = Format.getCompactFormat();
}
if (encoding != null) {
format.setEncoding(encoding);
}

View file

@ -168,7 +168,11 @@ public class XmlReader extends Reader {
*
*/
public XmlReader(final InputStream is, final boolean lenient, final String defaultEncoding) throws IOException, XmlReaderException {
this.defaultEncoding = defaultEncoding == null ? staticDefaultEncoding : defaultEncoding;
if (defaultEncoding == null) {
this.defaultEncoding = staticDefaultEncoding;
} else {
this.defaultEncoding = defaultEncoding;
}
try {
doRawStream(is, lenient);
} catch (final XmlReaderException ex) {
@ -339,7 +343,11 @@ public class XmlReader extends Reader {
*/
public XmlReader(final InputStream is, final String httpContentType, final boolean lenient, final String defaultEncoding) throws IOException,
XmlReaderException {
this.defaultEncoding = defaultEncoding == null ? staticDefaultEncoding : defaultEncoding;
if (defaultEncoding == null) {
this.defaultEncoding = staticDefaultEncoding;
} else {
this.defaultEncoding = defaultEncoding;
}
try {
doHttpStream(is, httpContentType, lenient);
} catch (final XmlReaderException ex) {
@ -408,7 +416,11 @@ public class XmlReader extends Reader {
encoding = ex.getContentTypeEncoding();
}
if (encoding == null) {
encoding = defaultEncoding == null ? UTF_8 : defaultEncoding;
if (defaultEncoding == null) {
encoding = UTF_8;
} else {
encoding = defaultEncoding;
}
}
prepareReader(ex.getInputStream(), encoding);
}
@ -472,7 +484,11 @@ public class XmlReader extends Reader {
String encoding;
if (bomEnc == null) {
if (xmlGuessEnc == null || xmlEnc == null) {
encoding = defaultEncoding == null ? UTF_8 : defaultEncoding;
if (defaultEncoding == null) {
encoding = UTF_8;
} else {
encoding = defaultEncoding;
}
} else if (xmlEnc.equals(UTF_16) && (xmlGuessEnc.equals(UTF_16BE) || xmlGuessEnc.equals(UTF_16LE))) {
encoding = xmlGuessEnc;
} else {
@ -514,7 +530,11 @@ public class XmlReader extends Reader {
if (appXml) {
encoding = calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc, is);
} else {
encoding = defaultEncoding == null ? US_ASCII : defaultEncoding;
if (defaultEncoding == null) {
encoding = US_ASCII;
} else {
encoding = defaultEncoding;
}
}
} else if (bomEnc != null && (cTEnc.equals(UTF_16BE) || cTEnc.equals(UTF_16LE))) {
throw new XmlReaderException(HTTP_EX_1.format(new Object[] { cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc }), cTMime, cTEnc, bomEnc,
@ -542,7 +562,11 @@ public class XmlReader extends Reader {
String mime = null;
if (httpContentType != null) {
final int i = httpContentType.indexOf(";");
mime = (i == -1 ? httpContentType : httpContentType.substring(0, i)).trim();
if (i == -1) {
mime = httpContentType.trim();
} else {
mime = httpContentType.substring(0, i).trim();
}
}
return mime;
}
@ -558,8 +582,12 @@ public class XmlReader extends Reader {
if (i > -1) {
final String postMime = httpContentType.substring(i + 1);
final Matcher m = CHARSET_PATTERN.matcher(postMime);
encoding = m.find() ? m.group(1) : null;
encoding = encoding != null ? encoding.toUpperCase(Locale.ENGLISH) : null;
if (m.find()) {
encoding = m.group(1);
}
if (encoding != null) {
encoding = encoding.toUpperCase(Locale.ENGLISH);
}
}
if (encoding != null && (encoding.startsWith("\"") && encoding.endsWith("\"") || encoding.startsWith("'") && encoding.endsWith("'"))) {
encoding = encoding.substring(1, encoding.length() - 1);

View file

@ -239,7 +239,9 @@ public class Atom03Parser extends BaseWireFeedParser {
private Content parseContent(final Element e) {
String value = null;
String type = getAttributeValue(e, "type");
type = type != null ? type : "text/plain";
if (type == null) {
type = "text/plain";
}
String mode = getAttributeValue(e, "mode");
if (mode == null) {
mode = Content.XML; // default to xml content

View file

@ -327,7 +327,9 @@ public class Atom10Parser extends BaseWireFeedParser {
private String parseTextConstructToString(final Element e) {
String value = null;
String type = getAttributeValue(e, "type");
type = type != null ? type : Content.TEXT;
if (type == null) {
type = Content.TEXT;
}
if (type.equals(Content.XHTML) || type.indexOf("/xml") != -1 || type.indexOf("+xml") != -1) {
// XHTML content needs special handling
final XMLOutputter outputter = new XMLOutputter();
@ -508,7 +510,9 @@ public class Atom10Parser extends BaseWireFeedParser {
return url;
}
if (isRelativeURI(url)) {
url = !".".equals(url) && !"./".equals(url) ? url : "";
if (".".equals(url) || "./".equals(url)) {
url = "";
}
if (url.startsWith("/") && baseURI != null) {
String base = null;

View file

@ -114,8 +114,16 @@ public class Base64 {
eData[eIndex++] = (byte) e1;
eData[eIndex++] = (byte) e2;
eData[eIndex++] = pad < 2 ? (byte) e3 : (byte) '=';
eData[eIndex++] = pad < 1 ? (byte) e4 : (byte) '=';
if (pad < 2) {
eData[eIndex++] = (byte) e3;
} else {
eData[eIndex++] = (byte) '=';
}
if (pad < 1) {
eData[eIndex++] = (byte) e4;
} else {
eData[eIndex++] = (byte) '=';
}
}
return eData;

View file

@ -105,7 +105,9 @@ public class DateParser {
*
*/
private static Date parseUsingMask(final String[] masks, String sDate) {
sDate = sDate != null ? sDate.trim() : null;
if (sDate != null) {
sDate = sDate.trim();
}
ParsePosition pp = null;
Date d = null;
for (int i = 0; d == null && i < masks.length; i++) {

View file

@ -145,7 +145,12 @@ public abstract class PluginManager {
final List classes = new ArrayList();
final boolean useLoadClass = Boolean.valueOf(System.getProperty("rome.pluginmanager.useloadclass", "false")).booleanValue();
for (final String propertyValue : propertyValues) {
final Class mClass = useLoadClass ? classLoader.loadClass(propertyValue) : Class.forName(propertyValue, true, classLoader);
final Class mClass;
if (useLoadClass) {
mClass = classLoader.loadClass(propertyValue);
} else {
mClass = Class.forName(propertyValue, true, classLoader);
}
classes.add(mClass);
}
final Class[] array = new Class[classes.size()];

View file

@ -186,14 +186,24 @@ public class TestXmlReader extends TestCase {
public void testAlternateDefaultEncoding(final String cT, final String bomEnc, final String streamEnc, final String prologEnc, final String alternateEnc)
throws Exception {
try {
final InputStream is = getXmlStream(bomEnc, prologEnc == null ? XML1 : XML3, streamEnc, prologEnc);
final InputStream is;
if (prologEnc == null) {
is = getXmlStream(bomEnc, XML1, streamEnc, prologEnc);
} else {
is = getXmlStream(bomEnc, XML3, streamEnc, prologEnc);
}
XmlReader.setDefaultEncoding(alternateEnc);
final XmlReader xmlReader = new XmlReader(is, cT, false);
if (!streamEnc.equals("UTF-16")) {
// we can not assert things here becuase UTF-8, US-ASCII and
// ISO-8859-1 look alike for the chars used for detection
} else {
final String enc = alternateEnc != null ? alternateEnc : streamEnc;
final String enc;
if (alternateEnc != null) {
enc = alternateEnc;
} else {
enc = streamEnc;
}
assertEquals(xmlReader.getEncoding().substring(0, streamEnc.length()), streamEnc);
}
} finally {
@ -202,7 +212,12 @@ public class TestXmlReader extends TestCase {
}
public void testHttpValid(final String cT, final String bomEnc, final String streamEnc, final String prologEnc) throws Exception {
final InputStream is = getXmlStream(bomEnc, prologEnc == null ? XML1 : XML3, streamEnc, prologEnc);
final InputStream is;
if (prologEnc == null) {
is = getXmlStream(bomEnc, XML1, streamEnc, prologEnc);
} else {
is = getXmlStream(bomEnc, XML3, streamEnc, prologEnc);
}
final XmlReader xmlReader = new XmlReader(is, cT, false);
if (!streamEnc.equals("UTF-16")) {
// we can not assert things here becuase UTF-8, US-ASCII and
@ -213,7 +228,12 @@ public class TestXmlReader extends TestCase {
}
protected void testHttpInvalid(final String cT, final String bomEnc, final String streamEnc, final String prologEnc) throws Exception {
final InputStream is = getXmlStream(bomEnc, prologEnc == null ? XML2 : XML3, streamEnc, prologEnc);
final InputStream is;
if (prologEnc == null) {
is = getXmlStream(bomEnc, XML2, streamEnc, prologEnc);
} else {
is = getXmlStream(bomEnc, XML3, streamEnc, prologEnc);
}
try {
new XmlReader(is, cT, false);
fail("It should have failed for HTTP Content-type " + cT + ", BOM " + bomEnc + ", streamEnc " + streamEnc + " and prologEnc " + prologEnc);
@ -224,7 +244,12 @@ public class TestXmlReader extends TestCase {
protected void testHttpLenient(final String cT, final String bomEnc, final String streamEnc, final String prologEnc, final String shouldbe)
throws Exception {
final InputStream is = getXmlStream(bomEnc, prologEnc == null ? XML2 : XML3, streamEnc, prologEnc);
final InputStream is;
if (prologEnc == null) {
is = getXmlStream(bomEnc, XML2, streamEnc, prologEnc);
} else {
is = getXmlStream(bomEnc, XML3, streamEnc, prologEnc);
}
final XmlReader xmlReader = new XmlReader(is, cT, true);
assertEquals(xmlReader.getEncoding(), shouldbe);
}