Path: blob/master/src/java.base/share/classes/sun/nio/fs/ExtendedOptions.java
41159 views
/*1* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.nio.file.CopyOption;28import java.nio.file.OpenOption;29import java.nio.file.WatchEvent;30import java.util.Map;31import java.util.concurrent.ConcurrentHashMap;3233/**34* Provides support for handling JDK-specific OpenOption, CopyOption and35* WatchEvent.Modifier types.36*/3738public final class ExtendedOptions {3940// maps InternalOption to ExternalOption41private static final Map<InternalOption<?>, Wrapper<?>> internalToExternal42= new ConcurrentHashMap<>();4344/**45* Wraps an option or modifier.46*/47private static final class Wrapper<T> {48private final Object option;49private final T param;5051Wrapper(Object option, T param) {52this.option = option;53this.param = param;54}5556T parameter() {57return param;58}59}6061/**62* The internal version of a JDK-specific OpenOption, CopyOption or63* WatchEvent.Modifier.64*/65public static final class InternalOption<T> {6667InternalOption() { }6869private void registerInternal(Object option, T param) {70Wrapper<T> wrapper = new Wrapper<T>(option, param);71internalToExternal.put(this, wrapper);72}7374/**75* Register this internal option as a OpenOption.76*/77public void register(OpenOption option) {78registerInternal(option, null);79}8081/**82* Register this internal option as a CopyOption.83*/84public void register(CopyOption option) {85registerInternal(option, null);86}8788/**89* Register this internal option as a WatchEvent.Modifier.90*/91public void register(WatchEvent.Modifier option) {92registerInternal(option, null);93}9495/**96* Register this internal option as a WatchEvent.Modifier with the97* given parameter.98*/99public void register(WatchEvent.Modifier option, T param) {100registerInternal(option, param);101}102103/**104* Returns true if the given option (or modifier) maps to this internal105* option.106*/107public boolean matches(Object option) {108Wrapper <?> wrapper = internalToExternal.get(this);109if (wrapper == null)110return false;111else112return option == wrapper.option;113}114115/**116* Returns the parameter object associated with this internal option.117*/118@SuppressWarnings("unchecked")119public T parameter() {120Wrapper<?> wrapper = internalToExternal.get(this);121if (wrapper == null)122return null;123else124return (T) wrapper.parameter();125}126}127128// Internal equivalents of the options and modifiers defined in129// package com.sun.nio.file130131public static final InternalOption<Void> INTERRUPTIBLE = new InternalOption<>();132133public static final InternalOption<Void> NOSHARE_READ = new InternalOption<>();134public static final InternalOption<Void> NOSHARE_WRITE = new InternalOption<>();135public static final InternalOption<Void> NOSHARE_DELETE = new InternalOption<>();136137public static final InternalOption<Void> FILE_TREE = new InternalOption<>();138139public static final InternalOption<Void> DIRECT = new InternalOption<>();140141public static final InternalOption<Integer> SENSITIVITY_HIGH = new InternalOption<>();142public static final InternalOption<Integer> SENSITIVITY_MEDIUM = new InternalOption<>();143public static final InternalOption<Integer> SENSITIVITY_LOW = new InternalOption<>();144}145146147