instanceof 관련 수정

master
mjkhan21 7 months ago
parent d0b16b7174
commit bfc2cd515b

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

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

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

@ -90,8 +90,8 @@ public class DataFormat extends AbstractComponent {
} catch (Exception e) { } catch (Exception e) {
return str; return str;
} }
} else if (obj instanceof Date) } else if (obj instanceof Date date)
return dateFormat.format((Date)obj); return dateFormat.format(date);
else else
throw new IllegalArgumentException(obj + " is not a Date"); throw new IllegalArgumentException(obj + " is not a Date");
} }
@ -106,10 +106,10 @@ public class DataFormat extends AbstractComponent {
Number num = null; Number num = null;
if (obj == null) if (obj == null)
num = 0; num = 0;
else if (obj instanceof String) else if (obj instanceof String str)
num = Double.valueOf(ifEmpty((String)obj, "0")); num = Double.valueOf(ifEmpty(str, "0"));
else if (obj instanceof Number) else if (obj instanceof Number n)
num = (Number)obj; num = n;
else else
throw new IllegalArgumentException(obj + " is not a number"); 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) { public static DataObject create(Map<String, ?> map) {
if (map == null) return null; if (map == null) return null;
if (map instanceof DataObject) return (DataObject)map; if (map instanceof DataObject dataObject) return dataObject;
DataObject result = new DataObject(); DataObject result = new DataObject();
map.forEach(result::put); map.forEach(result::put);
@ -88,7 +88,7 @@ public class DataObject extends StringMap<Object> {
*/ */
public String string(String name) { public String string(String name) {
Object obj = get(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 . /** Number .
@ -102,10 +102,10 @@ public class DataObject extends StringMap<Object> {
Object obj = get(name); Object obj = get(name);
if (obj == null) if (obj == null)
return Integer.valueOf(0); return Integer.valueOf(0);
if (obj instanceof Number) if (obj instanceof Number num)
return Number.class.cast(obj); return num;
if (obj instanceof String) if (obj instanceof String str)
return Double.valueOf((String)obj); return Double.valueOf(str);
throw new RuntimeException("The Object named '" + name + "' is not a Number"); throw new RuntimeException("The Object named '" + name + "' is not a Number");
} }
@ -121,7 +121,7 @@ public class DataObject extends StringMap<Object> {
if (obj == null) if (obj == null)
return false; 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(); return bool.booleanValue();
} }
} }

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save