jdk/src/share/classes/sun/nio/fs/AbstractBasicFileAttributeView.java
changeset 2057 3acf8e5e2ca0
child 3065 452aaa2899fc
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.nio.fs;
       
    27 
       
    28 import java.nio.file.attribute.*;
       
    29 import java.io.IOException;
       
    30 import java.util.*;
       
    31 import java.util.concurrent.TimeUnit;
       
    32 
       
    33 /**
       
    34  * Base implementation of BasicFileAttributeView
       
    35  */
       
    36 
       
    37 abstract class AbstractBasicFileAttributeView
       
    38     implements BasicFileAttributeView
       
    39 {
       
    40     private static final String SIZE_NAME = "size";
       
    41     private static final String CREATION_TIME_NAME = "creationTime";
       
    42     private static final String LAST_ACCESS_TIME_NAME = "lastAccessTime";
       
    43     private static final String LAST_MODIFIED_TIME_NAME = "lastModifiedTime";
       
    44     private static final String RESOLUTION_NAME = "resolution";
       
    45     private static final String FILE_KEY_NAME = "fileKey";
       
    46     private static final String LINK_COUNT_NAME = "linkCount";
       
    47     private static final String IS_DIRECTORY_NAME = "isDirectory";
       
    48     private static final String IS_REGULAR_FILE_NAME = "isRegularFile";
       
    49     private static final String IS_SYMBOLIC_LINK_NAME = "isSymbolicLink";
       
    50     private static final String IS_OTHER_NAME = "isOther";
       
    51 
       
    52     protected AbstractBasicFileAttributeView() { }
       
    53 
       
    54     @Override
       
    55     public String name() {
       
    56         return "basic";
       
    57     }
       
    58 
       
    59     @Override
       
    60     public Object getAttribute(String attribute) throws IOException {
       
    61         BasicFileAttributes attrs = readAttributes();
       
    62         if (attribute.equals(SIZE_NAME))
       
    63             return attrs.size();
       
    64         if (attribute.equals(CREATION_TIME_NAME))
       
    65             return attrs.creationTime();
       
    66         if (attribute.equals(LAST_ACCESS_TIME_NAME))
       
    67             return attrs.lastAccessTime();
       
    68         if (attribute.equals(LAST_MODIFIED_TIME_NAME))
       
    69             return attrs.lastModifiedTime();
       
    70         if (attribute.equals(RESOLUTION_NAME))
       
    71             return attrs.resolution();
       
    72         if (attribute.equals(FILE_KEY_NAME))
       
    73             return attrs.fileKey();
       
    74         if (attribute.equals(LINK_COUNT_NAME))
       
    75             return attrs.linkCount();
       
    76         if (attribute.equals(IS_DIRECTORY_NAME))
       
    77             return attrs.isDirectory();
       
    78         if (attribute.equals(IS_REGULAR_FILE_NAME))
       
    79             return attrs.isRegularFile();
       
    80         if (attribute.equals(IS_SYMBOLIC_LINK_NAME))
       
    81             return attrs.isSymbolicLink();
       
    82         if (attribute.equals(IS_OTHER_NAME))
       
    83             return attrs.isOther();
       
    84         return null;
       
    85     }
       
    86 
       
    87     private Long toTimeValue(Object value) {
       
    88         if (value == null)
       
    89             throw new NullPointerException();
       
    90         Long time = (Long)value;
       
    91         if (time < 0L && time != -1L)
       
    92             throw new IllegalArgumentException("time value cannot be negative");
       
    93         return time;
       
    94     }
       
    95 
       
    96     @Override
       
    97     public void setAttribute(String attribute, Object value)
       
    98         throws IOException
       
    99     {
       
   100         if (attribute.equals(LAST_MODIFIED_TIME_NAME)) {
       
   101             setTimes(toTimeValue(value), null, null, TimeUnit.MILLISECONDS);
       
   102             return;
       
   103         }
       
   104         if (attribute.equals(LAST_ACCESS_TIME_NAME)) {
       
   105             setTimes(null, toTimeValue(value), null, TimeUnit.MILLISECONDS);
       
   106             return;
       
   107         }
       
   108         if (attribute.equals(CREATION_TIME_NAME)) {
       
   109             setTimes(null, null, toTimeValue(value), TimeUnit.MILLISECONDS);
       
   110             return;
       
   111         }
       
   112         throw new UnsupportedOperationException("'" + attribute +
       
   113             "' is unknown or read-only attribute");
       
   114     }
       
   115 
       
   116     /**
       
   117      *
       
   118      */
       
   119     static class AttributesBuilder {
       
   120         private Set<String> set = new HashSet<String>();
       
   121         private Map<String,Object> map = new HashMap<String,Object>();
       
   122         private boolean copyAll;
       
   123 
       
   124         private AttributesBuilder(String first, String[] rest) {
       
   125             if (first.equals("*")) {
       
   126                 copyAll = true;
       
   127             } else {
       
   128                 set.add(first);
       
   129                 // copy names into the given Set bailing out if "*" is found
       
   130                 for (String attribute: rest) {
       
   131                     if (attribute.equals("*")) {
       
   132                         copyAll = true;
       
   133                         break;
       
   134                     }
       
   135                     set.add(attribute);
       
   136                 }
       
   137             }
       
   138         }
       
   139 
       
   140         /**
       
   141          * Creates builder to build up a map of the matching attributes
       
   142          */
       
   143         static AttributesBuilder create(String first, String[] rest) {
       
   144             return new AttributesBuilder(first, rest);
       
   145         }
       
   146 
       
   147         /**
       
   148          * Returns true if the attribute should be returned in the map
       
   149          */
       
   150         boolean match(String attribute) {
       
   151             if (copyAll)
       
   152                 return true;
       
   153             return set.contains(attribute);
       
   154         }
       
   155 
       
   156         void add(String attribute, Object value) {
       
   157             map.put(attribute, value);
       
   158         }
       
   159 
       
   160         /**
       
   161          * Returns the map. Discard all references to the AttributesBuilder
       
   162          * after invoking this method.
       
   163          */
       
   164         Map<String,Object> unmodifiableMap() {
       
   165             return Collections.unmodifiableMap(map);
       
   166         }
       
   167     }
       
   168 
       
   169     /**
       
   170      * Invoked by readAttributes or sub-classes to add all matching basic
       
   171      * attributes to the builder
       
   172      */
       
   173     final void addBasicAttributesToBuilder(BasicFileAttributes attrs,
       
   174                                            AttributesBuilder builder)
       
   175     {
       
   176         if (builder.match(SIZE_NAME))
       
   177             builder.add(SIZE_NAME, attrs.size());
       
   178         if (builder.match(CREATION_TIME_NAME))
       
   179             builder.add(CREATION_TIME_NAME, attrs.creationTime());
       
   180         if (builder.match(LAST_ACCESS_TIME_NAME))
       
   181             builder.add(LAST_ACCESS_TIME_NAME, attrs.lastAccessTime());
       
   182         if (builder.match(LAST_MODIFIED_TIME_NAME))
       
   183             builder.add(LAST_MODIFIED_TIME_NAME, attrs.lastModifiedTime());
       
   184         if (builder.match(RESOLUTION_NAME))
       
   185             builder.add(RESOLUTION_NAME, attrs.resolution());
       
   186         if (builder.match(FILE_KEY_NAME))
       
   187             builder.add(FILE_KEY_NAME, attrs.fileKey());
       
   188         if (builder.match(LINK_COUNT_NAME))
       
   189             builder.add(LINK_COUNT_NAME, attrs.linkCount());
       
   190         if (builder.match(IS_DIRECTORY_NAME))
       
   191             builder.add(IS_DIRECTORY_NAME, attrs.isDirectory());
       
   192         if (builder.match(IS_REGULAR_FILE_NAME))
       
   193             builder.add(IS_REGULAR_FILE_NAME, attrs.isRegularFile());
       
   194         if (builder.match(IS_SYMBOLIC_LINK_NAME))
       
   195             builder.add(IS_SYMBOLIC_LINK_NAME, attrs.isSymbolicLink());
       
   196         if (builder.match(IS_OTHER_NAME))
       
   197             builder.add(IS_OTHER_NAME, attrs.isOther());
       
   198     }
       
   199 
       
   200     @Override
       
   201     public Map<String,?> readAttributes(String first, String[] rest)
       
   202         throws IOException
       
   203     {
       
   204         AttributesBuilder builder = AttributesBuilder.create(first, rest);
       
   205         addBasicAttributesToBuilder(readAttributes(), builder);
       
   206         return builder.unmodifiableMap();
       
   207     }
       
   208 }