Skip to content
Browse files

Always return same instance

  • Loading branch information...
1 parent 0b899a7 commit 2b7b4173a15869838ff85c091ca5bb5975dba7a9 @ratnikov ratnikov committed Mar 4, 2014
Showing with 76 additions and 72 deletions.
  1. +76 −72 core/src/main/java/org/jruby/util/EmptyFileResource.java
View
148 core/src/main/java/org/jruby/util/EmptyFileResource.java
@@ -6,76 +6,80 @@
import org.jruby.runtime.ThreadContext;
class EmptyFileResource implements FileResource {
- public static EmptyFileResource create(String pathname) {
- return (pathname == null || "".equals(pathname)) ?
- new EmptyFileResource() : null;
- }
-
- @Override
- public String absolutePath() {
- return "";
- }
-
- @Override
- public boolean exists() {
- return false;
- }
-
- @Override
- public boolean isDirectory() {
- return false;
- }
-
- @Override
- public boolean isFile() {
- return false;
- }
-
- @Override
- public boolean canRead() {
- return false;
- }
-
- @Override
- public boolean canWrite() {
- return false;
- }
-
- @Override
- public boolean isSymLink() {
- return false;
- }
-
- @Override
- public String[] list() {
- return new String[0];
- }
-
- @Override
- public long lastModified() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public long length() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public FileStat stat(POSIX posix) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public FileStat lstat(POSIX posix) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public JRubyFile hackyGetJRubyFile() {
- // It is somewhat weird that we're returning the NOT_EXIST instance that this resource is
- // intending to replace. However, that should go away once we get rid of the hacky method, so
- // should be okay for now.
- return JRubyNonExistentFile.NOT_EXIST;
- }
+ // All empty resources are the same and immutable, so may as well
+ // cache the instance
+ private static final EmptyFileResource INSTANCE = new EmptyFileResource();
+
+ public static EmptyFileResource create(String pathname) {
+ return (pathname == null || "".equals(pathname)) ?
+ INSTANCE : null;
+ }
+
+ @Override
+ public String absolutePath() {
+ return "";
+ }
+
+ @Override
+ public boolean exists() {
+ return false;
+ }
+
+ @Override
+ public boolean isDirectory() {
+ return false;
+ }
+
+ @Override
+ public boolean isFile() {
+ return false;
+ }
+
+ @Override
+ public boolean canRead() {
+ return false;
+ }
+
+ @Override
+ public boolean canWrite() {
+ return false;
+ }
+
+ @Override
+ public boolean isSymLink() {
+ return false;
+ }
+
+ @Override
+ public String[] list() {
+ return new String[0];
+ }
+
+ @Override
+ public long lastModified() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public long length() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public FileStat stat(POSIX posix) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public FileStat lstat(POSIX posix) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public JRubyFile hackyGetJRubyFile() {
+ // It is somewhat weird that we're returning the NOT_EXIST instance that this resource is
+ // intending to replace. However, that should go away once we get rid of the hacky method, so
+ // should be okay for now.
+ return JRubyNonExistentFile.NOT_EXIST;
+ }
}

0 comments on commit 2b7b417

Please sign in to comment.
Something went wrong with that request. Please try again.