instanceof 관련 수정

master
mjkhan21 7 months ago
parent d0b16b7174
commit bfc2cd515b

@ -13,9 +13,7 @@ public class ApplicationException extends RuntimeException {
* @return ApplicationException
*/
public static ApplicationException get(Throwable cause) {
return cause instanceof ApplicationException ?
ApplicationException.class.cast(cause) :
new ApplicationException(cause);
return cause instanceof ApplicationException ae ? ae : new ApplicationException(cause);
}
private DataObject objs;

@ -18,12 +18,10 @@ public class Assert {
*/
public static final boolean isEmpty(Object obj) {
if (obj == null) return true;
if (obj instanceof String) {
String str = (String)obj;
return str.trim().isEmpty();
if (obj instanceof String str) {
return str.isEmpty() || str.isBlank();
}
if (obj instanceof Iterable) {
Iterable<?> objs = (Iterable<?>)obj;
if (obj instanceof Iterable<?> objs) {
return !objs.iterator().hasNext();
}
if (obj.getClass().isArray()) {
@ -94,8 +92,8 @@ public class Assert {
public static final Throwable rootCause(Throwable t) {
Throwable cause = t != null ? t.getCause() : null;
if (cause == null
&& t instanceof InvocationTargetException)
cause = ((InvocationTargetException)t).getTargetException();
&& t instanceof InvocationTargetException e)
cause = e.getTargetException();
return cause == null || cause == t ? t : rootCause(cause);
}
@ -105,7 +103,7 @@ public class Assert {
*/
public static final RuntimeException runtimeException(Throwable t) {
t = rootCause(t);
return t instanceof RuntimeException ? RuntimeException.class.cast(t) : new RuntimeException(t);
return t instanceof RuntimeException re ? re : new RuntimeException(t);
}
/**{@link Assert} .

@ -79,8 +79,7 @@ public class Convert {
return Integer.valueOf(0);
if (obj instanceof Number)
return Number.class.cast(obj);
if (obj instanceof String) {
String s = (String)obj;
if (obj instanceof String s) {
return Double.valueOf(s.replace(",", "").trim());
}
throw inconvertible(obj, Number.class);

@ -90,8 +90,8 @@ public class DataFormat extends AbstractComponent {
} catch (Exception e) {
return str;
}
} else if (obj instanceof Date)
return dateFormat.format((Date)obj);
} else if (obj instanceof Date date)
return dateFormat.format(date);
else
throw new IllegalArgumentException(obj + " is not a Date");
}
@ -106,10 +106,10 @@ public class DataFormat extends AbstractComponent {
Number num = null;
if (obj == null)
num = 0;
else if (obj instanceof String)
num = Double.valueOf(ifEmpty((String)obj, "0"));
else if (obj instanceof Number)
num = (Number)obj;
else if (obj instanceof String str)
num = Double.valueOf(ifEmpty(str, "0"));
else if (obj instanceof Number n)
num = n;
else
throw new IllegalArgumentException(obj + " is not a number");

@ -19,7 +19,7 @@ public class DataObject extends StringMap<Object> {
*/
public static DataObject create(Map<String, ?> map) {
if (map == null) return null;
if (map instanceof DataObject) return (DataObject)map;
if (map instanceof DataObject dataObject) return dataObject;
DataObject result = new DataObject();
map.forEach(result::put);
@ -88,7 +88,7 @@ public class DataObject extends StringMap<Object> {
*/
public String string(String name) {
Object obj = get(name);
return isEmpty(obj) ? "" : obj instanceof String ? (String)obj : obj.toString().trim();
return isEmpty(obj) ? "" : obj instanceof String str ? str : obj.toString().trim();
}
/** Number .
@ -102,10 +102,10 @@ public class DataObject extends StringMap<Object> {
Object obj = get(name);
if (obj == null)
return Integer.valueOf(0);
if (obj instanceof Number)
return Number.class.cast(obj);
if (obj instanceof String)
return Double.valueOf((String)obj);
if (obj instanceof Number num)
return num;
if (obj instanceof String str)
return Double.valueOf(str);
throw new RuntimeException("The Object named '" + name + "' is not a Number");
}
@ -121,7 +121,7 @@ public class DataObject extends StringMap<Object> {
if (obj == null)
return false;
Boolean bool = obj instanceof Boolean ? Boolean.class.cast(obj) : Boolean.valueOf(obj.toString());
Boolean bool = obj instanceof Boolean b ? b : Boolean.valueOf(obj.toString());
return bool.booleanValue();
}
}

@ -126,8 +126,8 @@ public class JSON extends AbstractComponent {
* @return JSON
*/
public String stringify(Object obj, boolean indent) {
if (obj instanceof String)
return (String)obj;
if (obj instanceof String str)
return str;
ObjectMapper mapper = getObjectMapper();
ObjectWriter writer = null;

@ -109,8 +109,8 @@ public class XML extends AbstractComponent {
* @return XML
*/
public String stringify(Object obj, boolean indent) {
if (obj instanceof String)
return (String)obj;
if (obj instanceof String str)
return str;
XmlMapper mapper = getXmlMapper();
ObjectWriter writer = !indent ? mapper.writer() : mapper.writerWithDefaultPrettyPrinter();

@ -55,8 +55,7 @@ public class BoundedList<E> extends ArrayList<E> {
if (first instanceof Map) {
Map<String, ?> row = (Map<String, ?>)first;
totalSize = Pageable.getTotalSize(row);
} else if (first instanceof Pageable) {
Pageable row = (Pageable)first;
} else if (first instanceof Pageable row) {
totalSize = row.getTotalSize();
}
}

@ -41,10 +41,9 @@ public class MapperSupport extends MybatisPlugin {
Object obj = super.handle(resultSetHandler, statement);
Pageable.Info paging = pagingInfo.get();
if (paging != null && (obj instanceof List)) {
if (paging != null && (obj instanceof List<?> list)) {
pagingInfo.remove();
List<?> list = (List<?>)obj;
BoundedList<Object> boundedList = new BoundedList<>();
boundedList.setFetchSize(paging.fetchSize);
boundedList.addAll(list);
@ -69,14 +68,12 @@ public class MapperSupport extends MybatisPlugin {
if (isEmpty(arg)) return;
now = ifEmpty(now, () -> dateFormat.format(new Date()));
if (arg instanceof AbstractEntity) {
setTimestamp(sqlType, (AbstractEntity)arg, now);
} else if (arg instanceof Map) {
Map<?, ?> map = (Map<?, ?>)arg;
if (arg instanceof AbstractEntity entity) {
setTimestamp(sqlType, entity, now);
} else if (arg instanceof Map<?, ?> map) {
for (Object o: map.values())
setEntityTimestamp(sqlType, o, now);
} else if (arg instanceof Iterable) {
Iterable<?> iterable = (Iterable<?>)arg;
} else if (arg instanceof Iterable<?> iterable) {
for (Object o: iterable)
setEntityTimestamp(sqlType, o, now);
} else if (arg.getClass().isArray()) {

@ -125,11 +125,9 @@ public class MybatisPlugin extends AbstractComponent implements Interceptor {
}
protected void process(Object obj, Consumer<Object> processor) {
if (obj instanceof Map) {
Map<?, ?> map = (Map<?, ?>)obj;
if (obj instanceof Map<?, ?> map) {
map.values().forEach(processor);
} else if (obj instanceof Iterable) {
Iterable<?> objs = (Iterable<?>)obj;
} else if (obj instanceof Iterable<?> objs) {
for (Object o: objs)
processor.accept(o);
} else if (obj.getClass().isArray()) {

@ -79,14 +79,12 @@ public interface Pageable {
Pageable.getPageNum(params),
Pageable.getFetchSize(params)
);
} else if (obj instanceof QueryRequest) {
QueryRequest req = (QueryRequest)obj;
} else if (obj instanceof QueryRequest req) {
return new Pageable.Info(
req.getPageNum(),
req.getFetchSize()
);
} else if (obj instanceof Pageable) {
Pageable pageable = (Pageable)obj;
} else if (obj instanceof Pageable pageable) {
return new Pageable.Info(
pageable.getPageNum(),
pageable.getFetchSize()

@ -32,8 +32,8 @@ public class ListSupport<T> {
*/
public static <T> List<T> asList(Iterable<T> iterable) {
return iterable == null ? Collections.emptyList() :
iterable instanceof List<T> ?
(List<T>)iterable :
iterable instanceof List<T> list ?
list :
StreamSupport.stream(iterable.spliterator(), false).toList();
}

@ -142,6 +142,7 @@ public abstract class AbstractController extends AbstractComponent {
* @return ModelAndView
*/
protected ModelAndView setCollectionInfo(ModelAndView mav, Collection<?> collection, String prefixKey, String prefix) {
boolean toJson = !"jsonView".equals(mav.getViewName());
mav.addObject(prefixKey, prefix)
.addObject(prefix + "List", collection)
.addObject(prefix + "Start", 0)
@ -173,8 +174,7 @@ public abstract class AbstractController extends AbstractComponent {
* @return ModelAndView
*/
protected ModelAndView setPagingInfo(ModelAndView mav, Collection<?> collection, String prefix) {
if (collection instanceof BoundedList) {
BoundedList<?> bounded = (BoundedList<?>)collection;
if (collection instanceof BoundedList<?> bounded) {
Map<String, Object> map = Map.of(
"start", bounded.getStart(),
"totalSize", bounded.getTotalSize(),

@ -20,8 +20,7 @@ import cokr.xit.foundation.Downloadable;
public class DownloadView extends AbstractView {
public static String getFilename(Map<String, Object> model) {
Object obj = model.get("download");
if (obj instanceof Downloadable) {
Downloadable downloadable = (Downloadable)obj;
if (obj instanceof Downloadable downloadable) {
return downloadable.getFilename();
}
return null;

@ -551,8 +551,7 @@ public class WebClient {
byteList.add(separator);
Object value = entry.getValue();
if (value instanceof Path) {
Path path = (Path)value;
if (value instanceof Path path) {
String mimeType = Files.probeContentType(path);
byteList.add(("\"" + entry.getKey() + "\"; filename=\"" + path.getFileName() + "\"\r\nContent-Type: " + mimeType + "\r\n\r\n").getBytes(charset));
byteList.add(Files.readAllBytes(path));

Loading…
Cancel
Save