aboutsummaryrefslogtreecommitdiffstats
path: root/java_jni/org/jau/fs/TraverseOptions.java
blob: 19722c4962fc8bd59555004aa436425c40d7990a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2022 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.jau.fs;

/**
 * Filesystem traverse options used to visit() path elements.
 *
 * This `enum class` type fulfills `C++ named requirements: BitmaskType`.
 *
 * @see FileUtil#visit(FileStats, TraverseOptions, org.jau.fs.FileUtil.PathVisitor)
 * @see FileUtil#remove(String, TraverseOptions)
 */
public class TraverseOptions {
    public static final TraverseOptions none = new TraverseOptions();
    public static final TraverseOptions recursive = new TraverseOptions(TraverseOptions.Bit.recursive.value);

    public enum Bit {
        /** No option set */
        none ( (short) 0 ),

        /** Traverse through directories, i.e. perform visit, copy, remove etc actions recursively throughout the directory structure. */
        recursive ( (short) ( 1 << 0 ) ),

        /** Traverse through symbolic linked directories if traverse_options::recursive is set, i.e. directories with property fmode_t::link set. */
        follow_symlinks ( (short) ( 1 << 1 ) ),

        /** Traverse through elements in lexicographical order. This might be required when computing an order dependent outcome like a hash value. */
        lexicographical_order ( (short) ( 1 << 2 ) ),

        /** Visit the content's parent directory at entry. Both, dir_entry and dir_exit can be set, only one or none. */
        dir_entry ( (short) ( 1 << 8 ) ),

        /** Visit the content's parent directory at exit. Both, dir_entry and dir_exit can be set, only one or none. */
        dir_exit ( (short) ( 1 << 9 ) ),

        /** Enable verbosity mode, potentially used by a path_visitor implementation like remove(). */
        verbose ( (short) ( 1 << 15 ) );

        Bit(final short v) { value = v; }
        public final short value;
    }
    public short mask;

    public TraverseOptions(final short v) {
        mask = v;
    }
    public TraverseOptions() {
        mask = 0;
    }

    public boolean isSet(final Bit bit) { return bit.value == ( mask & bit.value ); }

    /**
     * Sets the given bit and returns this instance for chaining.
     * @param bit the given Bit value to set
     * @return this instance for chaining.
     */
    public TraverseOptions set(final Bit bit) { mask = (short) ( mask | bit.value ); return this; }

    @Override
    public String toString() {
        int count = 0;
        final StringBuilder out = new StringBuilder();
        for (final Bit dt : Bit.values()) {
            if( isSet(dt) ) {
                if( 0 < count ) { out.append(", "); }
                out.append(dt.name()); count++;
            }
        }
        if( 1 < count ) {
            out.insert(0, "[");
            out.append("]");
        }
        return out.toString();
    }

    @Override
    public boolean equals(final Object other) {
        if (this == other) {
            return true;
        }
        return (other instanceof TraverseOptions) &&
               this.mask == ((TraverseOptions)other).mask;
    }
}