1 /*
   2  * Copyright (c) 2008, 2011, 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 
  26 package sun.nio.fs;
  27 
  28 import java.nio.file.attribute.*;
  29 import java.util.*;
  30 import java.io.IOException;
  31 
  32 /**
  33  * Base implementation of BasicFileAttributeView
  34  */
  35 
  36 abstract class AbstractBasicFileAttributeView
  37     implements BasicFileAttributeView, DynamicFileAttributeView
  38 {
  39     private static final String SIZE_NAME = "size";
  40     private static final String CREATION_TIME_NAME = "creationTime";
  41     private static final String LAST_ACCESS_TIME_NAME = "lastAccessTime";
  42     private static final String LAST_MODIFIED_TIME_NAME = "lastModifiedTime";
  43     private static final String FILE_KEY_NAME = "fileKey";
  44     private static final String IS_DIRECTORY_NAME = "isDirectory";
  45     private static final String IS_REGULAR_FILE_NAME = "isRegularFile";
  46     private static final String IS_SYMBOLIC_LINK_NAME = "isSymbolicLink";
  47     private static final String IS_OTHER_NAME = "isOther";
  48 
  49     // the names of the basic attributes
  50     static final Set<String> basicAttributeNames =
  51         Util.newSet(SIZE_NAME,
  52                     CREATION_TIME_NAME,
  53                     LAST_ACCESS_TIME_NAME,
  54                     LAST_MODIFIED_TIME_NAME,
  55                     FILE_KEY_NAME,
  56                     IS_DIRECTORY_NAME,
  57                     IS_REGULAR_FILE_NAME,
  58                     IS_SYMBOLIC_LINK_NAME,
  59                     IS_OTHER_NAME);
  60 
  61     protected AbstractBasicFileAttributeView() { }
  62 
  63     @Override
  64     public String name() {
  65         return "basic";
  66     }
  67 
  68     @Override
  69     public void setAttribute(String attribute, Object value)
  70         throws IOException
  71     {
  72         if (attribute.equals(LAST_MODIFIED_TIME_NAME)) {
  73             setTimes((FileTime)value, null, null);
  74             return;
  75         }
  76         if (attribute.equals(LAST_ACCESS_TIME_NAME)) {
  77             setTimes(null, (FileTime)value, null);
  78             return;
  79         }
  80         if (attribute.equals(CREATION_TIME_NAME)) {
  81             setTimes(null, null, (FileTime)value);
  82             return;
  83         }
  84         throw new IllegalArgumentException("'" + name() + ":" +
  85             attribute + "' not recognized");
  86     }
  87 
  88     /**
  89      * Used to build a map of attribute name/values.
  90      */
  91     static class AttributesBuilder {
  92         private Set<String> names = new HashSet<>();
  93         private Map<String,Object> map = new HashMap<>();
  94         private boolean copyAll;
  95 
  96         private AttributesBuilder(Set<String> allowed, String[] requested) {
  97             for (String name: requested) {
  98                 if (name.equals("*")) {
  99                     copyAll = true;
 100                 } else {
 101                     if (!allowed.contains(name))
 102                         throw new IllegalArgumentException("'" + name + "' not recognized");
 103                     names.add(name);
 104                 }
 105             }
 106         }
 107 
 108         /**
 109          * Creates builder to build up a map of the matching attributes
 110          */
 111         static AttributesBuilder create(Set<String> allowed, String[] requested) {
 112             return new AttributesBuilder(allowed, requested);
 113         }
 114 
 115         /**
 116          * Returns true if the attribute should be returned in the map
 117          */
 118         boolean match(String name) {
 119             return copyAll || names.contains(name);
 120         }
 121 
 122         void add(String name, Object value) {
 123             map.put(name, value);
 124         }
 125 
 126         /**
 127          * Returns the map. Discard all references to the AttributesBuilder
 128          * after invoking this method.
 129          */
 130         Map<String,Object> unmodifiableMap() {
 131             return Collections.unmodifiableMap(map);
 132         }
 133     }
 134 
 135     /**
 136      * Invoked by readAttributes or sub-classes to add all matching basic
 137      * attributes to the builder
 138      */
 139     final void addRequestedBasicAttributes(BasicFileAttributes attrs,
 140                                            AttributesBuilder builder)
 141     {
 142         if (builder.match(SIZE_NAME))
 143             builder.add(SIZE_NAME, attrs.size());
 144         if (builder.match(CREATION_TIME_NAME))
 145             builder.add(CREATION_TIME_NAME, attrs.creationTime());
 146         if (builder.match(LAST_ACCESS_TIME_NAME))
 147             builder.add(LAST_ACCESS_TIME_NAME, attrs.lastAccessTime());
 148         if (builder.match(LAST_MODIFIED_TIME_NAME))
 149             builder.add(LAST_MODIFIED_TIME_NAME, attrs.lastModifiedTime());
 150         if (builder.match(FILE_KEY_NAME))
 151             builder.add(FILE_KEY_NAME, attrs.fileKey());
 152         if (builder.match(IS_DIRECTORY_NAME))
 153             builder.add(IS_DIRECTORY_NAME, attrs.isDirectory());
 154         if (builder.match(IS_REGULAR_FILE_NAME))
 155             builder.add(IS_REGULAR_FILE_NAME, attrs.isRegularFile());
 156         if (builder.match(IS_SYMBOLIC_LINK_NAME))
 157             builder.add(IS_SYMBOLIC_LINK_NAME, attrs.isSymbolicLink());
 158         if (builder.match(IS_OTHER_NAME))
 159             builder.add(IS_OTHER_NAME, attrs.isOther());
 160     }
 161 
 162     @Override
 163     public Map<String,Object> readAttributes(String[] requested)
 164         throws IOException
 165     {
 166         AttributesBuilder builder =
 167             AttributesBuilder.create(basicAttributeNames, requested);
 168         addRequestedBasicAttributes(readAttributes(), builder);
 169         return builder.unmodifiableMap();
 170     }
 171 }