/** * Parse {@link PreserveAttributes} from a string of the form \[rbugp]*\: * * r -> preserve replication * * b -> preserve block size * * u -> preserve owner * * g -> preserve group * * p -> preserve permissions * Characters not in this character set will be ignored. * * @param s String of the form \[rbugp]*\ * @return Parsed {@link PreserveAttributes} */ public static PreserveAttributes fromMnemonicString(String s) { if (Strings.isNullOrEmpty(s)) { return new PreserveAttributes(0); } s = s.toLowerCase(); Preconditions.checkArgument(ATTRIBUTES_REGEXP.matcher(s).matches(), "Invalid %s string %s, must be of the form %s.", PreserveAttributes.class.getSimpleName(), s, ATTRIBUTES_REGEXP.pattern()); int value = 0; for (Option option : Option.values()) { if (s.indexOf(option.token) >= 0) { value |= 1 << option.ordinal(); } } return new PreserveAttributes(value); }
/** * Parse {@link PreserveAttributes} from a string of the form \[rbugp]*\: * * r -> preserve replication * * b -> preserve block size * * u -> preserve owner * * g -> preserve group * * p -> preserve permissions * Characters not in this character set will be ignored. * * @param s String of the form \[rbugp]*\ * @return Parsed {@link PreserveAttributes} */ public static PreserveAttributes fromMnemonicString(String s) { if (Strings.isNullOrEmpty(s)) { return new PreserveAttributes(0); } s = s.toLowerCase(); Preconditions.checkArgument(ATTRIBUTES_REGEXP.matcher(s).matches(), "Invalid %s string %s, must be of the form %s.", PreserveAttributes.class.getSimpleName(), s, ATTRIBUTES_REGEXP.pattern()); int value = 0; for (Option option : Option.values()) { if (s.indexOf(option.token) >= 0) { value |= 1 << option.ordinal(); } } return new PreserveAttributes(value); }
/** * Converts this instance of {@link PreserveAttributes} into a String that can be converted to an equivalent * {@link PreserveAttributes} using {@link PreserveAttributes#fromMnemonicString}. See the latter for more * information. * @return a String that can be converted to an equivalent {@link PreserveAttributes} using * {@link PreserveAttributes#fromMnemonicString} */ public String toMnemonicString() { int value = this.options; StringBuilder mnemonicString = new StringBuilder(); for (Option option : Option.values()) { if (value % 2 != 0) { mnemonicString.append(option.token); } value >>= 1; } return mnemonicString.toString(); }
/** * @return true if attribute should be preserved. */ public boolean preserve(Option option) { return 0 < (this.options & (1 << option.ordinal())); }
private static Pattern getAllowedRegexp() { StringBuilder builder = new StringBuilder("["); for (Option option : Option.values()) { builder.append(option.token); } builder.append("]*"); return Pattern.compile(builder.toString()); } }
@Test public void test() { Map<String, Set<PreserveAttributes.Option>> tests = Maps.newHashMap(); tests.put("r", Sets.newHashSet(Option.REPLICATION)); tests.put("b", Sets.newHashSet(Option.BLOCK_SIZE)); tests.put("u", Sets.newHashSet(Option.OWNER)); tests.put("g", Sets.newHashSet(Option.GROUP)); tests.put("p", Sets.newHashSet(Option.PERMISSION)); tests.put("ru", Sets.newHashSet(Option.REPLICATION, Option.OWNER)); tests.put("rbugp", Sets.newHashSet(Option.REPLICATION, Option.OWNER, Option.BLOCK_SIZE, Option.GROUP, Option.PERMISSION)); tests.put("rrr", Sets.newHashSet(Option.REPLICATION)); tests.put("rrb", Sets.newHashSet(Option.REPLICATION, Option.BLOCK_SIZE)); tests.put("", Sets.<Option>newHashSet()); for(Map.Entry<String, Set<PreserveAttributes.Option>> entry : tests.entrySet()) { PreserveAttributes preserve = PreserveAttributes.fromMnemonicString(entry.getKey()); for(Option option : Option.values()) { Assert.assertEquals(preserve.preserve(option), entry.getValue().contains(option)); } Assert.assertEquals(preserve, PreserveAttributes.fromMnemonicString(preserve.toMnemonicString())); } }
/** * Converts this instance of {@link PreserveAttributes} into a String that can be converted to an equivalent * {@link PreserveAttributes} using {@link PreserveAttributes#fromMnemonicString}. See the latter for more * information. * @return a String that can be converted to an equivalent {@link PreserveAttributes} using * {@link PreserveAttributes#fromMnemonicString} */ public String toMnemonicString() { int value = this.options; StringBuilder mnemonicString = new StringBuilder(); for (Option option : Option.values()) { if (value % 2 != 0) { mnemonicString.append(option.token); } value >>= 1; } return mnemonicString.toString(); }
private static Pattern getAllowedRegexp() { StringBuilder builder = new StringBuilder("["); for (Option option : Option.values()) { builder.append(option.token); } builder.append("]*"); return Pattern.compile(builder.toString()); } }
/** * @return true if attribute should be preserved. */ public boolean preserve(Option option) { return 0 < (this.options & (1 << option.ordinal())); }