Path: blob/master/src/java.base/share/classes/sun/nio/fs/AbstractBasicFileAttributeView.java
41159 views
/*1* Copyright (c) 2008, 2011, 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.attribute.*;28import java.util.*;29import java.io.IOException;3031/**32* Base implementation of BasicFileAttributeView33*/3435abstract class AbstractBasicFileAttributeView36implements BasicFileAttributeView, DynamicFileAttributeView37{38private static final String SIZE_NAME = "size";39private static final String CREATION_TIME_NAME = "creationTime";40private static final String LAST_ACCESS_TIME_NAME = "lastAccessTime";41private static final String LAST_MODIFIED_TIME_NAME = "lastModifiedTime";42private static final String FILE_KEY_NAME = "fileKey";43private static final String IS_DIRECTORY_NAME = "isDirectory";44private static final String IS_REGULAR_FILE_NAME = "isRegularFile";45private static final String IS_SYMBOLIC_LINK_NAME = "isSymbolicLink";46private static final String IS_OTHER_NAME = "isOther";4748// the names of the basic attributes49static final Set<String> basicAttributeNames =50Util.newSet(SIZE_NAME,51CREATION_TIME_NAME,52LAST_ACCESS_TIME_NAME,53LAST_MODIFIED_TIME_NAME,54FILE_KEY_NAME,55IS_DIRECTORY_NAME,56IS_REGULAR_FILE_NAME,57IS_SYMBOLIC_LINK_NAME,58IS_OTHER_NAME);5960protected AbstractBasicFileAttributeView() { }6162@Override63public String name() {64return "basic";65}6667@Override68public void setAttribute(String attribute, Object value)69throws IOException70{71if (attribute.equals(LAST_MODIFIED_TIME_NAME)) {72setTimes((FileTime)value, null, null);73return;74}75if (attribute.equals(LAST_ACCESS_TIME_NAME)) {76setTimes(null, (FileTime)value, null);77return;78}79if (attribute.equals(CREATION_TIME_NAME)) {80setTimes(null, null, (FileTime)value);81return;82}83throw new IllegalArgumentException("'" + name() + ":" +84attribute + "' not recognized");85}8687/**88* Used to build a map of attribute name/values.89*/90static class AttributesBuilder {91private Set<String> names = new HashSet<>();92private Map<String,Object> map = new HashMap<>();93private boolean copyAll;9495private AttributesBuilder(Set<String> allowed, String[] requested) {96for (String name: requested) {97if (name.equals("*")) {98copyAll = true;99} else {100if (!allowed.contains(name))101throw new IllegalArgumentException("'" + name + "' not recognized");102names.add(name);103}104}105}106107/**108* Creates builder to build up a map of the matching attributes109*/110static AttributesBuilder create(Set<String> allowed, String[] requested) {111return new AttributesBuilder(allowed, requested);112}113114/**115* Returns true if the attribute should be returned in the map116*/117boolean match(String name) {118return copyAll || names.contains(name);119}120121void add(String name, Object value) {122map.put(name, value);123}124125/**126* Returns the map. Discard all references to the AttributesBuilder127* after invoking this method.128*/129Map<String,Object> unmodifiableMap() {130return Collections.unmodifiableMap(map);131}132}133134/**135* Invoked by readAttributes or sub-classes to add all matching basic136* attributes to the builder137*/138final void addRequestedBasicAttributes(BasicFileAttributes attrs,139AttributesBuilder builder)140{141if (builder.match(SIZE_NAME))142builder.add(SIZE_NAME, attrs.size());143if (builder.match(CREATION_TIME_NAME))144builder.add(CREATION_TIME_NAME, attrs.creationTime());145if (builder.match(LAST_ACCESS_TIME_NAME))146builder.add(LAST_ACCESS_TIME_NAME, attrs.lastAccessTime());147if (builder.match(LAST_MODIFIED_TIME_NAME))148builder.add(LAST_MODIFIED_TIME_NAME, attrs.lastModifiedTime());149if (builder.match(FILE_KEY_NAME))150builder.add(FILE_KEY_NAME, attrs.fileKey());151if (builder.match(IS_DIRECTORY_NAME))152builder.add(IS_DIRECTORY_NAME, attrs.isDirectory());153if (builder.match(IS_REGULAR_FILE_NAME))154builder.add(IS_REGULAR_FILE_NAME, attrs.isRegularFile());155if (builder.match(IS_SYMBOLIC_LINK_NAME))156builder.add(IS_SYMBOLIC_LINK_NAME, attrs.isSymbolicLink());157if (builder.match(IS_OTHER_NAME))158builder.add(IS_OTHER_NAME, attrs.isOther());159}160161@Override162public Map<String,Object> readAttributes(String[] requested)163throws IOException164{165AttributesBuilder builder =166AttributesBuilder.create(basicAttributeNames, requested);167addRequestedBasicAttributes(readAttributes(), builder);168return builder.unmodifiableMap();169}170}171172173