893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
893N/Aimport java.nio.file.attribute.*;
1319N/Aimport java.util.*;
893N/Aimport java.io.IOException;
893N/A
893N/A/**
893N/A * Base implementation of BasicFileAttributeView
893N/A */
893N/A
893N/Aabstract class AbstractBasicFileAttributeView
1319N/A implements BasicFileAttributeView, DynamicFileAttributeView
893N/A{
893N/A private static final String SIZE_NAME = "size";
893N/A private static final String CREATION_TIME_NAME = "creationTime";
893N/A private static final String LAST_ACCESS_TIME_NAME = "lastAccessTime";
893N/A private static final String LAST_MODIFIED_TIME_NAME = "lastModifiedTime";
893N/A private static final String FILE_KEY_NAME = "fileKey";
893N/A private static final String IS_DIRECTORY_NAME = "isDirectory";
893N/A private static final String IS_REGULAR_FILE_NAME = "isRegularFile";
893N/A private static final String IS_SYMBOLIC_LINK_NAME = "isSymbolicLink";
893N/A private static final String IS_OTHER_NAME = "isOther";
893N/A
3779N/A // the names of the basic attributes
3779N/A static final Set<String> basicAttributeNames =
3779N/A Util.newSet(SIZE_NAME,
3779N/A CREATION_TIME_NAME,
3779N/A LAST_ACCESS_TIME_NAME,
3779N/A LAST_MODIFIED_TIME_NAME,
3779N/A FILE_KEY_NAME,
3779N/A IS_DIRECTORY_NAME,
3779N/A IS_REGULAR_FILE_NAME,
3779N/A IS_SYMBOLIC_LINK_NAME,
3779N/A IS_OTHER_NAME);
3779N/A
893N/A protected AbstractBasicFileAttributeView() { }
893N/A
893N/A @Override
893N/A public String name() {
893N/A return "basic";
893N/A }
893N/A
893N/A @Override
893N/A public void setAttribute(String attribute, Object value)
893N/A throws IOException
893N/A {
893N/A if (attribute.equals(LAST_MODIFIED_TIME_NAME)) {
1319N/A setTimes((FileTime)value, null, null);
893N/A return;
893N/A }
893N/A if (attribute.equals(LAST_ACCESS_TIME_NAME)) {
1319N/A setTimes(null, (FileTime)value, null);
893N/A return;
893N/A }
893N/A if (attribute.equals(CREATION_TIME_NAME)) {
1319N/A setTimes(null, null, (FileTime)value);
893N/A return;
893N/A }
3779N/A throw new IllegalArgumentException("'" + name() + ":" +
3779N/A attribute + "' not recognized");
893N/A }
893N/A
893N/A /**
1319N/A * Used to build a map of attribute name/values.
893N/A */
893N/A static class AttributesBuilder {
3779N/A private Set<String> names = new HashSet<>();
3471N/A private Map<String,Object> map = new HashMap<>();
893N/A private boolean copyAll;
893N/A
3779N/A private AttributesBuilder(Set<String> allowed, String[] requested) {
3779N/A for (String name: requested) {
3779N/A if (name.equals("*")) {
1319N/A copyAll = true;
1319N/A } else {
3779N/A if (!allowed.contains(name))
3779N/A throw new IllegalArgumentException("'" + name + "' not recognized");
3779N/A names.add(name);
893N/A }
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Creates builder to build up a map of the matching attributes
893N/A */
3779N/A static AttributesBuilder create(Set<String> allowed, String[] requested) {
3779N/A return new AttributesBuilder(allowed, requested);
893N/A }
893N/A
893N/A /**
893N/A * Returns true if the attribute should be returned in the map
893N/A */
3779N/A boolean match(String name) {
3779N/A return copyAll || names.contains(name);
893N/A }
893N/A
3779N/A void add(String name, Object value) {
3779N/A map.put(name, value);
893N/A }
893N/A
893N/A /**
893N/A * Returns the map. Discard all references to the AttributesBuilder
893N/A * after invoking this method.
893N/A */
893N/A Map<String,Object> unmodifiableMap() {
893N/A return Collections.unmodifiableMap(map);
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Invoked by readAttributes or sub-classes to add all matching basic
893N/A * attributes to the builder
893N/A */
3779N/A final void addRequestedBasicAttributes(BasicFileAttributes attrs,
893N/A AttributesBuilder builder)
893N/A {
893N/A if (builder.match(SIZE_NAME))
893N/A builder.add(SIZE_NAME, attrs.size());
893N/A if (builder.match(CREATION_TIME_NAME))
893N/A builder.add(CREATION_TIME_NAME, attrs.creationTime());
893N/A if (builder.match(LAST_ACCESS_TIME_NAME))
893N/A builder.add(LAST_ACCESS_TIME_NAME, attrs.lastAccessTime());
893N/A if (builder.match(LAST_MODIFIED_TIME_NAME))
893N/A builder.add(LAST_MODIFIED_TIME_NAME, attrs.lastModifiedTime());
893N/A if (builder.match(FILE_KEY_NAME))
893N/A builder.add(FILE_KEY_NAME, attrs.fileKey());
893N/A if (builder.match(IS_DIRECTORY_NAME))
893N/A builder.add(IS_DIRECTORY_NAME, attrs.isDirectory());
893N/A if (builder.match(IS_REGULAR_FILE_NAME))
893N/A builder.add(IS_REGULAR_FILE_NAME, attrs.isRegularFile());
893N/A if (builder.match(IS_SYMBOLIC_LINK_NAME))
893N/A builder.add(IS_SYMBOLIC_LINK_NAME, attrs.isSymbolicLink());
893N/A if (builder.match(IS_OTHER_NAME))
893N/A builder.add(IS_OTHER_NAME, attrs.isOther());
893N/A }
893N/A
893N/A @Override
3779N/A public Map<String,Object> readAttributes(String[] requested)
3779N/A throws IOException
3779N/A {
3779N/A AttributesBuilder builder =
3779N/A AttributesBuilder.create(basicAttributeNames, requested);
3779N/A addRequestedBasicAttributes(readAttributes(), builder);
893N/A return builder.unmodifiableMap();
893N/A }
893N/A}