jaulib v1.3.0
Jau Support Library (C++, Java, ..)
TraverseOptions.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24package org.jau.fs;
25
26/**
27 * Filesystem traverse options used to visit() path elements.
28 *
29 * This `enum class` type fulfills `C++ named requirements: BitmaskType`.
30 *
31 * @see FileUtil#visit(FileStats, TraverseOptions, org.jau.fs.FileUtil.PathVisitor)
32 * @see FileUtil#remove(String, TraverseOptions)
33 */
34public class TraverseOptions {
35 public static final TraverseOptions none = new TraverseOptions();
37
38 public enum Bit {
39 /** No option set */
40 none ( (short) 0 ),
41
42 /** Traverse through directories, i.e. perform visit, copy, remove etc actions recursively throughout the directory structure. */
43 recursive ( (short) ( 1 << 0 ) ),
44
45 /** Traverse through symbolic linked directories if traverse_options::recursive is set, i.e. directories with property fmode_t::link set. */
46 follow_symlinks ( (short) ( 1 << 1 ) ),
47
48 /** Traverse through elements in lexicographical order. This might be required when computing an order dependent outcome like a hash value. */
49 lexicographical_order ( (short) ( 1 << 2 ) ),
50
51 /** Call path_visitor at directory entry, allowing path_visitor to skip traversal of this directory if returning false. */
52 dir_check_entry ( (short) ( 1 << 7 ) ),
53
54 /** Visit the content's parent directory at entry. Both, dir_entry and dir_exit can be set, only one or none. */
55 dir_entry ( (short) ( 1 << 8 ) ),
56
57 /** Visit the content's parent directory at exit. Both, dir_entry and dir_exit can be set, only one or none. */
58 dir_exit ( (short) ( 1 << 9 ) ),
59
60 /** Enable verbosity mode, potentially used by a path_visitor implementation like remove(). */
61 verbose ( (short) ( 1 << 15 ) );
62
63 Bit(final short v) { value = v; }
64 public final short value;
65 }
66 public short mask;
67
68 public TraverseOptions(final short v) {
69 mask = v;
70 }
71 public TraverseOptions() {
72 mask = 0;
73 }
74
75 public boolean isSet(final Bit bit) { return bit.value == ( mask & bit.value ); }
76
77 /**
78 * Sets the given bit and returns this instance for chaining.
79 * @param bit the given Bit value to set
80 * @return this instance for chaining.
81 */
82 public TraverseOptions set(final Bit bit) { mask = (short) ( mask | bit.value ); return this; }
83
84 @Override
85 public String toString() {
86 int count = 0;
87 final StringBuilder out = new StringBuilder();
88 for (final Bit dt : Bit.values()) {
89 if( isSet(dt) ) {
90 if( 0 < count ) { out.append(", "); }
91 out.append(dt.name()); count++;
92 }
93 }
94 if( 1 < count ) {
95 out.insert(0, "[");
96 out.append("]");
97 }
98 return out.toString();
99 }
100
101 @Override
102 public boolean equals(final Object other) {
103 if (this == other) {
104 return true;
105 }
106 return (other instanceof TraverseOptions) &&
107 this.mask == ((TraverseOptions)other).mask;
108 }
109}
Filesystem traverse options used to visit() path elements.
boolean equals(final Object other)
boolean isSet(final Bit bit)
static final TraverseOptions recursive
static final TraverseOptions none
follow_symlinks
Traverse through symbolic linked directories if traverse_options::recursive is set,...
dir_entry
Visit the content's parent directory at entry.
lexicographical_order
Traverse through elements in lexicographical order.
dir_check_entry
Call path_visitor at directory entry, allowing path_visitor to skip traversal of this directory if re...
recursive
Traverse through directories, i.e.
verbose
Enable verbosity mode, potentially used by a path_visitor implementation like remove().
dir_exit
Visit the content's parent directory at exit.