You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/07/16 18:21:07 UTC
svn commit: r1362115 -
/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/PosixPermissions.java
Author: ggregory
Date: Mon Jul 16 16:21:06 2012
New Revision: 1362115
URL: http://svn.apache.org/viewvc?rev=1362115&view=rev
Log:
Makes new PosixPermission class immutable. Add missing Javadocs.
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/PosixPermissions.java
Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/PosixPermissions.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/PosixPermissions.java?rev=1362115&r1=1362114&r2=1362115&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/PosixPermissions.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/PosixPermissions.java Mon Jul 16 16:21:06 2012
@@ -17,13 +17,9 @@
package org.apache.commons.vfs2.util;
-import org.apache.commons.vfs2.FileSystemException;
-
-import java.io.IOException;
import java.util.EnumMap;
import java.util.Map;
-
/**
* UNIX permissions.
*
@@ -32,193 +28,268 @@ import java.util.Map;
public class PosixPermissions
{
-
+ /**
+ * Permission types.
+ */
static public enum Type
{
- // User rights
+ /**
+ * User right readable.
+ */
UserReadable(00400),
+
+ /**
+ * User right writable.
+ */
UserWritable(00200),
+
+ /**
+ * User right executable.
+ */
UserExecutable(00100),
- // Group rights
+ /**
+ * Group right readable.
+ */
GroupReadable(00040),
+
+ /**
+ * Group right writable.
+ */
GroupWritable(00020),
+
+ /**
+ * Group right executable.
+ */
GroupExecutable(00010),
- // Other rights
+ /**
+ * Other right readable.
+ */
OtherReadable(00004),
+
+ /**
+ * Other right writable.
+ */
OtherWritable(00002),
+
+ /**
+ * Other right executable.
+ */
OtherExecutable(00001);
final private int mask;
/**
- * Initialise with the mask
+ * Initialize with the mask
*/
- private Type(int mask)
+ private Type(final int mask)
{
this.mask = mask;
}
/**
- * Return the mask for this permission
+ * Return the mask for this permission.
+ *
+ * @return the mask for this permission.
*/
public int getMask()
{
- return mask;
+ return this.mask;
}
}
/**
- * Current permissions
+ * Current permissions.
*/
- int permissions;
+ private final int permissions;
/**
- * If the user is the owner of the file
+ * If the user is the owner of the file.
*/
- boolean isOwner;
+ private final boolean isOwner;
/**
- * If one user group is the group of the file
+ * If one user group is the group of the file.
*/
- boolean isInGroup;
-
+ private final boolean isInGroup;
/**
- * Creates a new PosixPermissions object
- * @param permissions The permissions
- * @param isOwner true if the user is the owner of the file
- * @param isInGroup true if the user is a group owner of the file
+ * Creates a new PosixPermissions object.
+ *
+ * @param permissions
+ * The permissions
+ * @param isOwner
+ * true if the user is the owner of the file
+ * @param isInGroup
+ * true if the user is a group owner of the file
*/
- public PosixPermissions(int permissions, boolean isOwner, boolean isInGroup)
+ public PosixPermissions(final int permissions, final boolean isOwner, final boolean isInGroup)
{
this.permissions = permissions;
this.isOwner = isOwner;
this.isInGroup = isInGroup;
}
- public int getPermissions()
- {
- return permissions;
- }
-
-
/**
- * Computes new permission from old ones
- *
- * @param values The permissions to set
- * @return The new permission
+ * Computes new permission from old ones.
+ *
+ * @param values
+ * The permissions to set.
+ * @return The new permissions.
*/
- private int computeNewPermissions(Map<Type, Boolean> values)
+ private int computeNewPermissions(final Map<Type, Boolean> values)
{
- int old = this.permissions;
- for (Map.Entry<Type, Boolean> entry : values.entrySet())
+ int newPerms = this.permissions;
+ for (final Map.Entry<Type, Boolean> entry : values.entrySet())
{
final Type type = entry.getKey();
if (entry.getValue())
{
- old |= type.getMask();
+ newPerms |= type.getMask();
} else
{
- old &= ~type.getMask();
+ newPerms &= ~type.getMask();
}
}
-
- return old;
+ return newPerms;
}
+ /**
+ * Tests whether the bit corresponding to the permission is set.
+ *
+ * @return whether the bit corresponding to the permission is set.
+ */
+ private boolean get(final Type type)
+ {
+ return (type.getMask() & this.permissions) != 0;
+ }
/**
- * Test whether the bit corresponding to the permission is set
+ * Gets permissions.
+ *
+ * @return permissions.
*/
- private boolean get(Type type)
+ public int getPermissions()
{
- return (type.getMask() & permissions) != 0;
+ return this.permissions;
}
/**
- * Check if whether the user can read the file
- *
- * @return true if the user can read
+ * Gets whether the permissions are executable.
+ *
+ * @return whether the permissions are executable.
*/
- public boolean isReadable()
+ public boolean isExecutable()
{
- if (isOwner)
+ if (this.isOwner)
{
- return get(Type.UserReadable);
+ return this.get(Type.UserExecutable);
}
- if (isInGroup)
+ if (this.isInGroup)
{
- return get(Type.GroupReadable);
+ return this.get(Type.GroupExecutable);
}
- return get(Type.OtherReadable);
+ return this.get(Type.OtherExecutable);
}
-
- public Integer makeReadable(boolean readable, boolean ownerOnly)
+ /**
+ * Gets whether the permissions are readable.
+ *
+ * @return whether the permissions are readable.
+ */
+ public boolean isReadable()
{
- EnumMap<Type, Boolean> map = new EnumMap<Type, Boolean>(Type.class);
- map.put(Type.UserReadable, readable);
- if (!ownerOnly)
+ if (this.isOwner)
{
- map.put(Type.GroupReadable, readable);
- map.put(Type.OtherReadable, readable);
+ return this.get(Type.UserReadable);
}
-
- return this.computeNewPermissions(map);
+ if (this.isInGroup)
+ {
+ return this.get(Type.GroupReadable);
+ }
+ return this.get(Type.OtherReadable);
}
+ /**
+ * Gets whether the permissions are writable.
+ *
+ * @return whether the permissions are writable.
+ */
public boolean isWritable()
{
- if (isOwner)
+ if (this.isOwner)
{
- return get(Type.UserWritable);
+ return this.get(Type.UserWritable);
}
- if (isInGroup)
+ if (this.isInGroup)
{
- return get(Type.GroupWritable);
+ return this.get(Type.GroupWritable);
}
- return get(Type.OtherWritable);
+ return this.get(Type.OtherWritable);
}
- public Integer makeWritable(boolean writable, boolean ownerOnly)
+ /**
+ * Creates new permissions based on these permissions.
+ *
+ * @param executable
+ * Whether the new permissions should be readable.
+ * @param ownerOnly
+ * Whether the new permissions are only for the owner.
+ * @return the new permissions.
+ */
+ public int makeExecutable(final boolean executable, final boolean ownerOnly)
{
- EnumMap<Type, Boolean> map = new EnumMap<Type, Boolean>(Type.class);
- map.put(Type.UserWritable, writable);
+ final EnumMap<Type, Boolean> map = new EnumMap<Type, Boolean>(Type.class);
+ map.put(Type.UserExecutable, executable);
if (!ownerOnly)
{
- map.put(Type.GroupWritable, writable);
- map.put(Type.OtherWritable, writable);
+ map.put(Type.GroupExecutable, executable);
+ map.put(Type.OtherExecutable, executable);
}
-
return this.computeNewPermissions(map);
}
- public boolean isExecutable()
+ /**
+ * Creates new permissions based on these permissions.
+ *
+ * @param readable
+ * Whether the new permissions should be readable.
+ * @param ownerOnly
+ * Whether the new permissions are only for the owner.
+ * @return the new permissions.
+ */
+ public Integer makeReadable(final boolean readable, final boolean ownerOnly)
{
- if (isOwner)
- {
- return get(Type.UserExecutable);
- }
- if (isInGroup)
+ final EnumMap<Type, Boolean> map = new EnumMap<Type, Boolean>(Type.class);
+ map.put(Type.UserReadable, readable);
+ if (!ownerOnly)
{
- return get(Type.GroupExecutable);
+ map.put(Type.GroupReadable, readable);
+ map.put(Type.OtherReadable, readable);
}
- return get(Type.OtherExecutable);
+ return this.computeNewPermissions(map);
}
- public int makeExecutable(boolean executable, boolean ownerOnly)
+ /**
+ * Creates new permissions based on these permissions.
+ *
+ * @param writable
+ * Whether the new permissions should be readable.
+ * @param ownerOnly
+ * Whether the new permissions are only for the owner.
+ * @return the new permissions.
+ */
+ public Integer makeWritable(final boolean writable, final boolean ownerOnly)
{
- EnumMap<Type, Boolean> map = new EnumMap<Type, Boolean>(Type.class);
- map.put(Type.UserExecutable, executable);
+ final EnumMap<Type, Boolean> map = new EnumMap<Type, Boolean>(Type.class);
+ map.put(Type.UserWritable, writable);
if (!ownerOnly)
{
- map.put(Type.GroupExecutable, executable);
- map.put(Type.OtherExecutable, executable);
+ map.put(Type.GroupWritable, writable);
+ map.put(Type.OtherWritable, writable);
}
-
return this.computeNewPermissions(map);
}
}