Skip to content

Commit

Permalink
Pattern Matching for "instanceof" operator should be used instead of …
Browse files Browse the repository at this point in the history
…simple "instanceof"
  • Loading branch information
groldan committed Dec 2, 2023
1 parent 3c79c8a commit f8ef31e
Show file tree
Hide file tree
Showing 54 changed files with 329 additions and 359 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ private ServiceInstance toService(org.springframework.cloud.client.ServiceInstan
}

private String getStatus(org.springframework.cloud.client.ServiceInstance i) {
if (i instanceof EurekaServiceInstance) {
EurekaServiceInstance e = (EurekaServiceInstance) i;
if (i instanceof EurekaServiceInstance e) {
InstanceInfo instanceInfo = e.getInstanceInfo();
InstanceStatus status = instanceInfo.getStatus();
return status.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ logging:
org.geoserver.cloud.config.factory: warn
org.geoserver.jdbcconfig: warn
org.geoserver.jdbcstore: warn
org.geoserver.cloud.catalog.locking: debug
org.geoserver.cloud.catalog.locking: info
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,16 @@ public boolean evict(CatalogInfo info) {
return evicted;
}

if (info instanceof ResourceInfo) {
CatalogInfoKey layersByResourceKey = generateLayersByResourceKey((ResourceInfo) info);
if (info instanceof ResourceInfo ri) {
CatalogInfoKey layersByResourceKey = generateLayersByResourceKey(ri);
idCache.evict(layersByResourceKey);
} else if (info instanceof LayerInfo) {
LayerInfo l = (LayerInfo) info;
} else if (info instanceof LayerInfo l) {
ResourceInfo r = l.getResource();
if (r != null) {
CatalogInfoKey layersByResourceKey = generateLayersByResourceKey(r);
idCache.evict(layersByResourceKey);
}
} else if (info instanceof WorkspaceInfo) {
WorkspaceInfo workspace = (WorkspaceInfo) info;
} else if (info instanceof WorkspaceInfo workspace) {
idCache.evictIfPresent(CachingCatalogFacade.generateDefaultDataStoreKey(workspace));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public boolean evict(Info info) {
return true;
}
}
if (info instanceof ServiceInfo) {
ServiceInfo service = (ServiceInfo) info;
if (info instanceof ServiceInfo service) {
Object idKey = CachingGeoServerFacade.serviceByIdKey(service.getId());
ValueWrapper cachedValue = cache.get(idKey);
if (cachedValue != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ CacheConfigurationPostProcessor cacheConfigurationPostProcessor() {
@Bean
CachingCatalogFacade cachingCatalogFacade(
@Qualifier("catalogFacade") CatalogFacade rawCatalogFacade) {
CatalogFacade raw = rawCatalogFacade;
ExtendedCatalogFacade facade;
if (raw instanceof ExtendedCatalogFacade) {
facade = (ExtendedCatalogFacade) rawCatalogFacade;
if (rawCatalogFacade instanceof ExtendedCatalogFacade ecf) {
facade = ecf;
} else {
facade = new CatalogFacadeExtensionAdapter(raw);
facade = new CatalogFacadeExtensionAdapter(rawCatalogFacade);
}
return new CachingCatalogFacadeImpl(facade);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public Patch resolve(Patch patch) {
}

private Object resolvePatchPropertyValue(Object orig) {
if (orig instanceof Info) {
return resolve((Info) orig);
if (orig instanceof Info info) {
return resolve(info);
}
if (orig instanceof List) {
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -245,8 +245,8 @@ protected void resolveInternal(LayerInfo layer) {
}

protected <T extends PublishedInfo> T resolveInternal(T published) {
if (published instanceof LayerInfo) resolve((LayerInfo) published);
else if (published instanceof LayerGroupInfo) resolve((LayerGroupInfo) published);
if (published instanceof LayerInfo li) resolve(li);
else if (published instanceof LayerGroupInfo lg) resolve(lg);
return published;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,9 @@ public AllowAllCatalogFacade(CatalogFacade facade) {
public BlockingCatalog(@Qualifier("rawCatalog") Catalog rawCatalog) {
super(rawCatalog);
// Make sure isolated workspaces can be created/updated
if (rawCatalog instanceof org.geoserver.catalog.plugin.CatalogPlugin) {
CatalogPlugin impl = ((org.geoserver.catalog.plugin.CatalogPlugin) rawCatalog);
impl.getRawFacade().getCatalogCapabilities().setIsolatedWorkspacesSupport(true);
} else if (rawCatalog instanceof org.geoserver.catalog.impl.CatalogImpl) {
org.geoserver.catalog.impl.CatalogImpl impl =
((org.geoserver.catalog.impl.CatalogImpl) rawCatalog);
if (rawCatalog instanceof org.geoserver.catalog.plugin.CatalogPlugin plugin) {
plugin.getRawFacade().getCatalogCapabilities().setIsolatedWorkspacesSupport(true);
} else if (rawCatalog instanceof org.geoserver.catalog.impl.CatalogImpl impl) {
impl.setFacade(new AllowAllCatalogFacade(impl.getFacade()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public StoreControllerTest() {
assertEquals(expected.getType(), actual.getType());
assertEquals(expected.getWorkspace(), actual.getWorkspace());
assertEquals(expected.isEnabled(), actual.isEnabled());
if (expected instanceof CoverageStoreInfo)
if (expected instanceof CoverageStoreInfo store)
assertEquals(
((CoverageStoreInfo) expected).getURL(), ((CoverageStoreInfo) actual).getURL());
if (expected instanceof HTTPStoreInfo)
store.getURL(), ((CoverageStoreInfo) actual).getURL());
if (expected instanceof HTTPStoreInfo httpStore)
assertEquals(
((HTTPStoreInfo) expected).getCapabilitiesURL(),
httpStore.getCapabilitiesURL(),
((HTTPStoreInfo) actual).getCapabilitiesURL());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ public InfoEventResolver(@NonNull Catalog rawCatalog, @NonNull GeoServer geoserv

@SuppressWarnings({"rawtypes", "unchecked"})
public InfoEvent<?, ?> resolve(InfoEvent event) {
if (event instanceof InfoAdded) {
InfoAdded addEvent = (InfoAdded) event;
if (event instanceof InfoAdded addEvent) {
Info object = addEvent.getObject();
addEvent.setObject(resolve(object));
} else if (event instanceof InfoModified) {
InfoModified modifyEvent = (InfoModified) event;
} else if (event instanceof InfoModified modifyEvent) {
modifyEvent.setPatch(resolve(modifyEvent.getPatch()));
}
return event;
Expand All @@ -68,8 +66,8 @@ public InfoEventResolver(@NonNull Catalog rawCatalog, @NonNull GeoServer geoserv
@SuppressWarnings("unchecked")
private <I extends Info> I resolve(I object) {
if (object == null) return null;
if (object instanceof CatalogInfo) {
return (I) resolve((CatalogInfo) object);
if (object instanceof CatalogInfo i) {
return (I) resolve(i);
}
return (I) configInfoResolver.apply(object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ private void publishRemoteEvent(RemoteGeoServerEvent remoteEvent) {
protected void logOutgoing(RemoteGeoServerEvent remoteEvent) {
@NonNull GeoServerEvent<?> event = remoteEvent.getEvent();
String logMsg = "{}: broadcasting {}";
if (event instanceof InfoModified) {
Patch patch = ((InfoModified<?, ?>) event).getPatch();
if (event instanceof InfoModified modEvent) {
Patch patch = modEvent.getPatch();
if (patch.isEmpty()) {
logMsg = "{}: broadcasting no-change event {}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public GeoServerEvent<?> toLocalRemote(@NonNull RemoteGeoServerEvent incoming) {
GeoServerEvent<?> event = incoming.getEvent();
event.setRemote(true);
event.setOrigin(incoming.getOriginService());
if (event instanceof InfoEvent)
event = remoteEventsPropertyResolver.resolve((InfoEvent<?, ?>) event);
if (event instanceof InfoEvent infoEvent)
event = remoteEventsPropertyResolver.resolve(infoEvent);
return event;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,7 @@ protected <T extends Info> void assertRemoteEvent(T info, RemoteGeoServerEvent b
}
assertThat(infoType.isInstance(info)).isTrue();

if (event instanceof InfoAdded) {
InfoAdded e = (InfoAdded) event;
if (event instanceof InfoAdded e) {
assertThat(e.getObject()).isNotNull();
assertThat(infoType.isInstance(e.getObject())).isTrue();
assertThat(e.getObject().getId()).isEqualTo(info.getId());
Expand All @@ -353,8 +352,7 @@ protected <T extends Info> void assertRemoteEvent(T info, RemoteGeoServerEvent b
// testData.assertEqualsLenientConnectionParameters(info, object);
}

if (event instanceof InfoModified) {
InfoModified modifyEvent = (InfoModified) event;
if (event instanceof InfoModified modifyEvent) {
assertThat(modifyEvent.getPatch()).isNotNull();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ public interface CatalogInfoMapper {
@SuppressWarnings("unchecked")
default <I extends CatalogInfo> I map(CatalogInfoDto dto) {
if (dto == null) return null;
if (dto instanceof Workspace) return (I) WORKSPACE_MAPPER.map((Workspace) dto);
if (dto instanceof Namespace) return (I) NAMESPACE_MAPPER.map((Namespace) dto);
if (dto instanceof Store) return (I) STORE_MAPPER.map((Store) dto);
if (dto instanceof Resource) return (I) RESOURCE_MAPPER.map((Resource) dto);
if (dto instanceof Published) return (I) PUBLISHED_MAPPER.map((Published) dto);
if (dto instanceof Style) return (I) STYLE_MAPPER.map((Style) dto);
if (dto instanceof Map) return (I) MAP_MAPPER.map((Map) dto);
if (dto instanceof Workspace ws) return (I) WORKSPACE_MAPPER.map(ws);
if (dto instanceof Namespace ns) return (I) NAMESPACE_MAPPER.map(ns);
if (dto instanceof Store store) return (I) STORE_MAPPER.map(store);
if (dto instanceof Resource res) return (I) RESOURCE_MAPPER.map(res);
if (dto instanceof Published published) return (I) PUBLISHED_MAPPER.map(published);
if (dto instanceof Style style) return (I) STYLE_MAPPER.map(style);
if (dto instanceof Map map) return (I) MAP_MAPPER.map(map);

throw new IllegalArgumentException(
"Unknown CatalogInfoDto type: " + dto.getClass().getCanonicalName());
}

default CatalogInfoDto map(CatalogInfo info) {
if (info == null) return null;
if (info instanceof WorkspaceInfo) return WORKSPACE_MAPPER.map((WorkspaceInfo) info);
if (info instanceof NamespaceInfo) return NAMESPACE_MAPPER.map((NamespaceInfo) info);
if (info instanceof StoreInfo) return STORE_MAPPER.map((StoreInfo) info);
if (info instanceof ResourceInfo) return RESOURCE_MAPPER.map((ResourceInfo) info);
if (info instanceof PublishedInfo) return PUBLISHED_MAPPER.map((PublishedInfo) info);
if (info instanceof StyleInfo) return STYLE_MAPPER.map((StyleInfo) info);
if (info instanceof MapInfo) return MAP_MAPPER.map((MapInfo) info);
if (info instanceof WorkspaceInfo ws) return WORKSPACE_MAPPER.map(ws);
if (info instanceof NamespaceInfo ns) return NAMESPACE_MAPPER.map(ns);
if (info instanceof StoreInfo store) return STORE_MAPPER.map(store);
if (info instanceof ResourceInfo res) return RESOURCE_MAPPER.map(res);
if (info instanceof PublishedInfo published) return PUBLISHED_MAPPER.map(published);
if (info instanceof StyleInfo style) return STYLE_MAPPER.map(style);
if (info instanceof MapInfo map) return MAP_MAPPER.map(map);
if (info instanceof CatalogInfo) return null;
throw new IllegalArgumentException(
"Unknown CatalogInfo type: " + info.getClass().getCanonicalName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ public interface PublishedMapper {

default PublishedInfo map(Published dto) {
if (dto == null) return null;
if (dto instanceof Layer) return map((Layer) dto);
if (dto instanceof LayerGroup) return map((LayerGroup) dto);
if (dto instanceof Layer l) return map(l);
if (dto instanceof LayerGroup lg) return map(lg);

throw new IllegalArgumentException(
"Unknown Published type: " + dto.getClass().getCanonicalName());
}

default Published map(PublishedInfo info) {
if (info == null) return null;
if (info instanceof LayerInfo) return map((LayerInfo) info);
if (info instanceof LayerGroupInfo) return map((LayerGroupInfo) info);
if (info instanceof LayerInfo l) return map(l);
if (info instanceof LayerGroupInfo lg) return map(lg);

throw new IllegalArgumentException(
"Unknown PublishedInfo type: " + info.getClass().getCanonicalName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public interface ResourceMapper {

default Resource map(ResourceInfo o) {
if (o == null) return null;
if (o instanceof FeatureTypeInfo) return map((FeatureTypeInfo) o);
if (o instanceof CoverageInfo) return map((CoverageInfo) o);
if (o instanceof WMSLayerInfo) return map((WMSLayerInfo) o);
if (o instanceof WMTSLayerInfo) return map((WMTSLayerInfo) o);
if (o instanceof FeatureTypeInfo ft) return map(ft);
if (o instanceof CoverageInfo cov) return map(cov);
if (o instanceof WMSLayerInfo wms) return map(wms);
if (o instanceof WMTSLayerInfo wmts) return map(wmts);
throw new IllegalArgumentException("Unknown ResourceInfo type: " + o);
}

default ResourceInfo map(Resource o) {
if (o == null) return null;
if (o instanceof FeatureType) return map((FeatureType) o);
if (o instanceof Coverage) return map((Coverage) o);
if (o instanceof WMSLayer) return map((WMSLayer) o);
if (o instanceof WMTSLayer) return map((WMTSLayer) o);
if (o instanceof FeatureType ft) return map(ft);
if (o instanceof Coverage cov) return map(cov);
if (o instanceof WMSLayer wms) return map(wms);
if (o instanceof WMTSLayer wmts) return map(wmts);
throw new IllegalArgumentException("Unknown Resource type: " + o);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ public interface StoreMapper {

default Store map(StoreInfo o) {
if (o == null) return null;
if (o instanceof DataStoreInfo) return map((DataStoreInfo) o);
if (o instanceof CoverageStoreInfo) return map((CoverageStoreInfo) o);
if (o instanceof WMSStoreInfo) return map((WMSStoreInfo) o);
if (o instanceof WMTSStoreInfo) return map((WMTSStoreInfo) o);
if (o instanceof DataStoreInfo ds) return map(ds);
if (o instanceof CoverageStoreInfo cs) return map(cs);
if (o instanceof WMSStoreInfo wms) return map(wms);
if (o instanceof WMTSStoreInfo wmts) return map(wmts);

throw new IllegalArgumentException("Unknown StoreInfo type: " + o);
}

default StoreInfo map(Store o) {
if (o == null) return null;
if (o instanceof DataStore) return map((DataStore) o);
if (o instanceof CoverageStore) return map((CoverageStore) o);
if (o instanceof WMSStore) return map((WMSStore) o);
if (o instanceof WMTSStore) return map((WMTSStore) o);
if (o instanceof DataStore ds) return map(ds);
if (o instanceof CoverageStore cs) return map(cs);
if (o instanceof WMSStore wms) return map(wms);
if (o instanceof WMTSStore wmts) return map(wmts);

throw new IllegalArgumentException("Unknown Store type: " + o);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ default GridGeometryDto gridGeometry2DToDto(GridGeometry value) {

default double[] affineTransform(MathTransform tx) {
double[] flatmatrix = null;
if (tx instanceof AffineTransform) {
AffineTransform atx = (AffineTransform) tx;
if (tx instanceof AffineTransform atx) {
flatmatrix = new double[6];
atx.getMatrix(flatmatrix);
}
Expand Down Expand Up @@ -265,8 +264,7 @@ default Locale stringToLocale(String s) {
}

default Map<String, String> internationalStringToDto(InternationalString s) {
if (s instanceof GrowableInternationalString) {
GrowableInternationalString gs = (GrowableInternationalString) s;
if (s instanceof GrowableInternationalString gs) {
Set<Locale> locales = gs.getLocales();
Map<String, String> dto = new HashMap<>(locales.size());
locales.forEach(locale -> dto.put(localeToString(locale), gs.toString(locale)));
Expand Down
Loading

0 comments on commit f8ef31e

Please sign in to comment.