01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.persistence;
20:
21: import java.util.EnumSet;
22:
23: import org.apache.openjpa.kernel.AutoDetach;
24:
25: /**
26: * The possible settings for the auto-detach behavior of an
27: * {@link OpenJPAEntityManager}.
28: *
29: * @since 1.0.0
30: * @published
31: */
32: public enum AutoDetachType {
33: CLOSE(AutoDetach.DETACH_CLOSE), COMMIT(AutoDetach.DETACH_COMMIT), NON_TRANSACTIONAL_READ(
34: AutoDetach.DETACH_NONTXREAD), ROLLBACK(
35: AutoDetach.DETACH_ROLLBACK);
36:
37: private final int autoDetachConstant;
38:
39: private AutoDetachType(int value) {
40: autoDetachConstant = value;
41: }
42:
43: public static EnumSet<AutoDetachType> toEnumSet(int autoDetach) {
44: EnumSet<AutoDetachType> types = EnumSet
45: .noneOf(AutoDetachType.class);
46: if ((autoDetach & AutoDetach.DETACH_CLOSE) != 0)
47: types.add(CLOSE);
48: if ((autoDetach & AutoDetach.DETACH_COMMIT) != 0)
49: types.add(COMMIT);
50: if ((autoDetach & AutoDetach.DETACH_NONTXREAD) != 0)
51: types.add(NON_TRANSACTIONAL_READ);
52: if ((autoDetach & AutoDetach.DETACH_ROLLBACK) != 0)
53: types.add(ROLLBACK);
54: return types;
55: }
56:
57: public static int fromEnumSet(EnumSet<AutoDetachType> types) {
58: int autoDetach = 0;
59: for (AutoDetachType type : types)
60: autoDetach |= type.autoDetachConstant;
61: return autoDetach;
62: }
63: }
|