1 /*
   2  * Copyright (c) 2008, 2009, 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     protected AbstractBasicFileAttributeView() { }
  50 
  51     @Override
  52     public String name() {
  53         return "basic";
  54     }
  55 
  56     @Override
  57     public void setAttribute(String attribute, Object value)
  58         throws IOException
  59     {
  60         if (attribute.equals(LAST_MODIFIED_TIME_NAME)) {
  61             setTimes((FileTime)value, null, null);
  62             return;
  63         }
  64         if (attribute.equals(LAST_ACCESS_TIME_NAME)) {
  65             setTimes(null, (FileTime)value, null);
  66             return;
  67         }
  68         if (attribute.equals(CREATION_TIME_NAME)) {
  69             setTimes(null, null, (FileTime)value);
  70             return;
  71         }
  72         throw new UnsupportedOperationException("'" + attribute +
  73             "' is unknown or read-only attribute");
  74     }
  75 
  76     /**
  77      * Used to build a map of attribute name/values.
  78      */
  79     static class AttributesBuilder {
  80         private Set<String> set = new HashSet<>();
  81         private Map<String,Object> map = new HashMap<>();
  82         private boolean copyAll;
  83 
  84         private AttributesBuilder(String[] attributes) {
  85             for (String attribute: attributes) {
  86                 if (attribute.equals("*")) {
  87                     copyAll = true;
  88                 } else {
  89                     set.add(attribute);
  90                 }
  91             }
  92         }
  93 
  94         /**
  95          * Creates builder to build up a map of the matching attributes
  96          */
  97         static AttributesBuilder create(String[] attributes) {
  98             return new AttributesBuilder(attributes);
  99         }
 100 
 101         /**
 102          * Returns true if the attribute should be returned in the map
 103          */
 104         boolean match(String attribute) {
 105             if (copyAll)
 106                 return true;
 107             return set.contains(attribute);
 108         }
 109 
 110         void add(String attribute, Object value) {
 111             map.put(attribute, value);
 112         }
 113 
 114         /**
 115          * Returns the map. Discard all references to the AttributesBuilder
 116          * after invoking this method.
 117          */
 118         Map<String,Object> unmodifiableMap() {
 119             return Collections.unmodifiableMap(map);
 120         }
 121     }
 122 
 123     /**
 124      * Invoked by readAttributes or sub-classes to add all matching basic
 125      * attributes to the builder
 126      */
 127     final void addBasicAttributesToBuilder(BasicFileAttributes attrs,
 128                                            AttributesBuilder builder)
 129     {
 130         if (builder.match(SIZE_NAME))
 131             builder.add(SIZE_NAME, attrs.size());
 132         if (builder.match(CREATION_TIME_NAME))
 133             builder.add(CREATION_TIME_NAME, attrs.creationTime());
 134         if (builder.match(LAST_ACCESS_TIME_NAME))
 135             builder.add(LAST_ACCESS_TIME_NAME, attrs.lastAccessTime());
 136         if (builder.match(LAST_MODIFIED_TIME_NAME))
 137             builder.add(LAST_MODIFIED_TIME_NAME, attrs.lastModifiedTime());
 138         if (builder.match(FILE_KEY_NAME))
 139             builder.add(FILE_KEY_NAME, attrs.fileKey());
 140         if (builder.match(IS_DIRECTORY_NAME))
 141             builder.add(IS_DIRECTORY_NAME, attrs.isDirectory());
 142         if (builder.match(IS_REGULAR_FILE_NAME))
 143             builder.add(IS_REGULAR_FILE_NAME, attrs.isRegularFile());
 144         if (builder.match(IS_SYMBOLIC_LINK_NAME))
 145             builder.add(IS_SYMBOLIC_LINK_NAME, attrs.isSymbolicLink());
 146         if (builder.match(IS_OTHER_NAME))
 147             builder.add(IS_OTHER_NAME, attrs.isOther());
 148     }
 149 
 150     @Override
 151     public Map<String,Object> readAttributes(String[] attributes) throws IOException {
 152         AttributesBuilder builder = AttributesBuilder.create(attributes);
 153         addBasicAttributesToBuilder(readAttributes(), builder);
 154         return builder.unmodifiableMap();
 155     }
 156 }