diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /config-lib/src/main/java/com/yahoo/config/FileReference.java |
Publish
Diffstat (limited to 'config-lib/src/main/java/com/yahoo/config/FileReference.java')
-rwxr-xr-x | config-lib/src/main/java/com/yahoo/config/FileReference.java | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/config-lib/src/main/java/com/yahoo/config/FileReference.java b/config-lib/src/main/java/com/yahoo/config/FileReference.java new file mode 100755 index 00000000000..de86fc09be7 --- /dev/null +++ b/config-lib/src/main/java/com/yahoo/config/FileReference.java @@ -0,0 +1,67 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * An immutable file reference that can only be created from classes within the same package. + * This is to prevent clients from creating arbitrary and invalid file references. + * + * @author tonytv + */ +public final class FileReference { + + private final String value; + + FileReference(String value) { + this.value = value; + } + + public String value() { + return value; + } + + @Override + public int hashCode() { + return value.hashCode(); + } + + @Override + public boolean equals(Object other) { + return other instanceof FileReference && + value.equals(((FileReference)other).value); + } + + @Override + public String toString() { + return "file '" + value + "'"; + } + + public static List<String> toValues(Collection<FileReference> references) { + List<String> ret = new ArrayList<String>(); + for (FileReference r: references) { + ret.add(r.value()); + } + return ret; + } + + public static Map<String, String> toValueMap(Map<String, FileReference> map) { + Map<String, String> ret = new LinkedHashMap<>(); + for (Map.Entry<String, FileReference> e : map.entrySet()) { + ret.put(e.getKey(), e.getValue().value()); + } + return ret; + } + + public static FileReference mockFileReferenceForUnitTesting(File file) { + if (! file.exists()) + throw new IllegalArgumentException("File '" + file.getAbsolutePath() + "' does not exist."); + return new FileReference(file.getPath()); + } + +} |