1 | /**
|
---|
2 | * UserAppliedPermission.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 | package org.expeditee.items;
|
---|
19 |
|
---|
20 | public enum UserAppliedPermission {
|
---|
21 | denied(-1), none(0), followLinks(1), copy(2), createFrames(3), full(4);
|
---|
22 |
|
---|
23 | private final int val;
|
---|
24 |
|
---|
25 | private UserAppliedPermission(final int val) {
|
---|
26 | this.val = val;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public static UserAppliedPermission getPermission(String permissionCode, final UserAppliedPermission defaultPermission) {
|
---|
30 | if (permissionCode == null) {
|
---|
31 | return defaultPermission;
|
---|
32 | }
|
---|
33 |
|
---|
34 | permissionCode = permissionCode.trim().toLowerCase();
|
---|
35 | if (permissionCode.length() == 0) {
|
---|
36 | return defaultPermission;
|
---|
37 | }
|
---|
38 |
|
---|
39 | try {
|
---|
40 | return resolve(Integer.parseInt(permissionCode), defaultPermission);
|
---|
41 | } catch (final NumberFormatException e) {
|
---|
42 | try {
|
---|
43 | return valueOf(permissionCode);
|
---|
44 | } catch (final Exception ex) {
|
---|
45 | return defaultPermission;
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public int getCode() {
|
---|
51 | return val;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public String toString() {
|
---|
55 | return this.name();
|
---|
56 | }
|
---|
57 |
|
---|
58 | public static UserAppliedPermission min(UserAppliedPermission p1, UserAppliedPermission p2) {
|
---|
59 | return p1.val < p2.val ? p1 : p2;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static UserAppliedPermission max(UserAppliedPermission p1, UserAppliedPermission p2) {
|
---|
63 | return p1.val > p2.val ? p1 : p2;
|
---|
64 | }
|
---|
65 |
|
---|
66 | private static UserAppliedPermission resolve(final int val, final UserAppliedPermission defaultPermission) {
|
---|
67 | for (UserAppliedPermission p : UserAppliedPermission.values()) {
|
---|
68 | if (p.val == val) {
|
---|
69 | return p;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return defaultPermission;
|
---|
73 | }
|
---|
74 | } |
---|