1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.nio.fs;
27
28import java.nio.file.CopyOption;
29import java.nio.file.OpenOption;
30import java.nio.file.WatchEvent;
31import java.util.Map;
32import java.util.concurrent.ConcurrentHashMap;
33
34/**
35 * Provides support for handling JDK-specific OpenOption, CopyOption and
36 * WatchEvent.Modifier types.
37 */
38
39public final class ExtendedOptions {
40
41    // maps InternalOption to ExternalOption
42    private static final Map<InternalOption<?>, Wrapper<?>> internalToExternal
43        = new ConcurrentHashMap<>();
44
45    /**
46     * Wraps an option or modifier.
47     */
48    private static final class Wrapper<T> {
49        private final Object option;
50        private final T param;
51
52        Wrapper(Object option, T param) {
53            this.option = option;
54            this.param = param;
55        }
56
57        T parameter() {
58            return param;
59        }
60    }
61
62    /**
63     * The internal version of a JDK-specific OpenOption, CopyOption or
64     * WatchEvent.Modifier.
65     */
66    public static final class InternalOption<T> {
67
68        InternalOption() { }
69
70        private void registerInternal(Object option, T param) {
71            Wrapper<T> wrapper = new Wrapper<T>(option, param);
72            internalToExternal.put(this, wrapper);
73        }
74
75        /**
76         * Register this internal option as a OpenOption.
77         */
78        public void register(OpenOption option) {
79            registerInternal(option, null);
80        }
81
82        /**
83         * Register this internal option as a CopyOption.
84         */
85        public void register(CopyOption option) {
86            registerInternal(option, null);
87        }
88
89        /**
90         * Register this internal option as a WatchEvent.Modifier.
91         */
92        public void register(WatchEvent.Modifier option) {
93            registerInternal(option, null);
94        }
95
96        /**
97         * Register this internal option as a WatchEvent.Modifier with the
98         * given parameter.
99         */
100        public void register(WatchEvent.Modifier option, T param) {
101            registerInternal(option, param);
102        }
103
104        /**
105         * Returns true if the given option (or modifier) maps to this internal
106         * option.
107         */
108        public boolean matches(Object option) {
109            Wrapper <?> wrapper = internalToExternal.get(this);
110            if (wrapper == null)
111                return false;
112            else
113                return option == wrapper.option;
114        }
115
116        /**
117         * Returns the parameter object associated with this internal option.
118         */
119        @SuppressWarnings("unchecked")
120        public T parameter() {
121            Wrapper<?> wrapper = internalToExternal.get(this);
122            if (wrapper == null)
123                return null;
124            else
125                return (T) wrapper.parameter();
126        }
127    }
128
129    // Internal equivalents of the options and modifiers defined in
130    // package com.sun.nio.file
131
132    public static final InternalOption<Void> INTERRUPTIBLE = new InternalOption<>();
133
134    public static final InternalOption<Void> NOSHARE_READ = new InternalOption<>();
135    public static final InternalOption<Void> NOSHARE_WRITE = new InternalOption<>();
136    public static final InternalOption<Void> NOSHARE_DELETE = new InternalOption<>();
137
138    public static final InternalOption<Void> FILE_TREE = new InternalOption<>();
139
140    public static final InternalOption<Integer> SENSITIVITY_HIGH = new InternalOption<>();
141    public static final InternalOption<Integer> SENSITIVITY_MEDIUM = new InternalOption<>();
142    public static final InternalOption<Integer> SENSITIVITY_LOW = new InternalOption<>();
143}
144