From b2761e03005e6fa6671a73e2aaceab0c6ae951f9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Fri, 3 Aug 2012 17:02:56 +0300 Subject: [PATCH] Add shorthand for avoiding null-checks with ResourceReference (#9211) --- .../terminal/gwt/server/ResourceReference.java | 16 ++++++++++++++++ src/com/vaadin/ui/AbstractComponent.java | 13 ++----------- src/com/vaadin/ui/Video.java | 4 ++-- .../v7a2/ResourceInStateComponent.java | 7 +------ 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/server/ResourceReference.java b/src/com/vaadin/terminal/gwt/server/ResourceReference.java index 98ac7e0f4b..2104ad4b87 100644 --- a/src/com/vaadin/terminal/gwt/server/ResourceReference.java +++ b/src/com/vaadin/terminal/gwt/server/ResourceReference.java @@ -48,4 +48,20 @@ public class ResourceReference extends URLReference { } } + + public static ResourceReference create(Resource resource) { + if (resource == null) { + return null; + } else { + return new ResourceReference(resource); + } + } + + public static Resource getResource(URLReference reference) { + if (reference == null) { + return null; + } + assert reference instanceof ResourceReference; + return ((ResourceReference) reference).getResource(); + } } diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index b71948e96e..e7cb38256c 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -294,12 +294,7 @@ public abstract class AbstractComponent extends AbstractClientConnector */ @Override public Resource getIcon() { - ResourceReference ref = ((ResourceReference) getState().getIcon()); - if (ref == null) { - return null; - } else { - return ref.getResource(); - } + return ResourceReference.getResource(getState().getIcon()); } /** @@ -311,11 +306,7 @@ public abstract class AbstractComponent extends AbstractClientConnector */ @Override public void setIcon(Resource icon) { - if (icon == null) { - getState().setIcon(null); - } else { - getState().setIcon(new ResourceReference(icon)); - } + getState().setIcon(ResourceReference.create(icon)); requestRepaint(); } diff --git a/src/com/vaadin/ui/Video.java b/src/com/vaadin/ui/Video.java index 48fe3e4192..d4f95a5be3 100644 --- a/src/com/vaadin/ui/Video.java +++ b/src/com/vaadin/ui/Video.java @@ -67,7 +67,7 @@ public class Video extends AbstractMedia { * @param poster */ public void setPoster(Resource poster) { - getState().setPoster(new ResourceReference(poster)); + getState().setPoster(ResourceReference.create(poster)); requestRepaint(); } @@ -75,7 +75,7 @@ public class Video extends AbstractMedia { * @return The poster image. */ public Resource getPoster() { - return ((ResourceReference) getState().getPoster()).getResource(); + return ResourceReference.getResource(getState().getPoster()); } } diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a2/ResourceInStateComponent.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a2/ResourceInStateComponent.java index f9d4cb38e6..b47f555640 100644 --- a/tests/testbench/com/vaadin/tests/minitutorials/v7a2/ResourceInStateComponent.java +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a2/ResourceInStateComponent.java @@ -20,11 +20,6 @@ public class ResourceInStateComponent extends AbstractComponent { } public Resource getMyIcon() { - ResourceReference ref = ((ResourceReference) getState().getMyIcon()); - if (ref != null) { - return ref.getResource(); - } else { - return null; - } + return ResourceReference.getResource(getState().getMyIcon()); } } -- 2.39.5