jdk/src/share/classes/sun/nio/fs/AbstractFileStoreSpaceAttributeView.java
changeset 2057 3acf8e5e2ca0
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 
       
    32 /**
       
    33  * Base implementation of FileStoreSpaceAttributeView
       
    34  */
       
    35 
       
    36 abstract class AbstractFileStoreSpaceAttributeView
       
    37     implements FileStoreSpaceAttributeView
       
    38 {
       
    39     private static final String TOTAL_SPACE_NAME = "totalSpace";
       
    40     private static final String USABLE_SPACE_NAME = "usableSpace";
       
    41     private static final String UNALLOCATED_SPACE_NAME = "unallocatedSpace";
       
    42 
       
    43     @Override
       
    44     public final String name() {
       
    45         return "space";
       
    46     }
       
    47 
       
    48     @Override
       
    49     public final Object getAttribute(String attribute) throws IOException {
       
    50         FileStoreSpaceAttributes attrs = readAttributes();
       
    51         if (attribute.equals(TOTAL_SPACE_NAME))
       
    52             return attrs.totalSpace();
       
    53         if (attribute.equals(USABLE_SPACE_NAME))
       
    54             return attrs.usableSpace();
       
    55         if (attribute.equals(UNALLOCATED_SPACE_NAME))
       
    56             return attrs.unallocatedSpace();
       
    57         return null;
       
    58     }
       
    59 
       
    60     @Override
       
    61     public final void setAttribute(String attribute, Object value)
       
    62         throws IOException
       
    63     {
       
    64         if (attribute == null || value == null)
       
    65             throw new NullPointerException();
       
    66         throw new UnsupportedOperationException();
       
    67     }
       
    68 
       
    69     @Override
       
    70     public final Map<String,?> readAttributes(String first, String[] rest)
       
    71         throws IOException
       
    72     {
       
    73         boolean total = false;
       
    74         boolean usable = false;
       
    75         boolean unallocated = false;
       
    76 
       
    77         if (first.equals(TOTAL_SPACE_NAME)) total = true;
       
    78         else if (first.equals(USABLE_SPACE_NAME)) usable = true;
       
    79         else if (first.equals(UNALLOCATED_SPACE_NAME)) unallocated = true;
       
    80         else if (first.equals("*")) {
       
    81             total = true;
       
    82             usable = true;
       
    83             unallocated = true;
       
    84         }
       
    85 
       
    86         if (!total || !usable || !unallocated) {
       
    87             for (String attribute: rest) {
       
    88                 if (attribute.equals("*")) {
       
    89                     total = true;
       
    90                     usable = true;
       
    91                     unallocated = true;
       
    92                     break;
       
    93                 }
       
    94                 if (attribute.equals(TOTAL_SPACE_NAME)) {
       
    95                     total = true;
       
    96                     continue;
       
    97                 }
       
    98                 if (attribute.equals(USABLE_SPACE_NAME)) {
       
    99                     usable = true;
       
   100                     continue;
       
   101                 }
       
   102                 if (attribute.equals(UNALLOCATED_SPACE_NAME)) {
       
   103                     unallocated = true;
       
   104                     continue;
       
   105                 }
       
   106             }
       
   107         }
       
   108 
       
   109         FileStoreSpaceAttributes attrs = readAttributes();
       
   110         Map<String,Object> result = new HashMap<String,Object>(2);
       
   111         if (total)
       
   112             result.put(TOTAL_SPACE_NAME, attrs.totalSpace());
       
   113         if (usable)
       
   114             result.put(USABLE_SPACE_NAME, attrs.usableSpace());
       
   115         if (unallocated)
       
   116             result.put(UNALLOCATED_SPACE_NAME, attrs.unallocatedSpace());
       
   117         return Collections.unmodifiableMap(result);
       
   118     }
       
   119 }