1 /* 2 * #%L 3 * settings4j 4 * =============================================================== 5 * Copyright (C) 2008 - 2015 Brabenetz Harald, Austria 6 * =============================================================== 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * #L% 19 */ 20 package org.settings4j; 21 22 import org.settings4j.settings.position.ConnectorPositionAfterFirstClass; 23 import org.settings4j.settings.position.ConnectorPositionAfterLastClass; 24 import org.settings4j.settings.position.ConnectorPositionAfterName; 25 import org.settings4j.settings.position.ConnectorPositionAtFirst; 26 import org.settings4j.settings.position.ConnectorPositionAtLast; 27 import org.settings4j.settings.position.ConnectorPositionBeforeFirstClass; 28 import org.settings4j.settings.position.ConnectorPositionBeforeLastClass; 29 import org.settings4j.settings.position.ConnectorPositionBeforeName; 30 import org.settings4j.settings.position.ConnectorPositionFirstValid; 31 32 33 /** 34 * ConnectorPostitions used for {@link Settings4jInstance#addConnector(Connector, ConnectorPosition)}. 35 * <h3>Exampe Usage:</h3> 36 * 37 * <pre> 38 * -------------------------------------- 39 * Connector myConnector = ... 40 * if (Settings4j.getSettings().getConnector(myConnector.getName()) == null) { 41 * Settings4j.getSettings().addConnector(myConnector, ConnectorPositions.firstValid(// 42 * ConnectorPositions.afterLast(SystemPropertyConnector.class), 43 * ConnectorPositions.atFirst() // fallback if no SystemPropertyConnector exists. 44 * ) 45 * ); 46 * } 47 * -------------------------------------- 48 * </pre> 49 * <p> 50 * It is even more readable with static import: 51 * </p> 52 * 53 * <pre> 54 * -------------------------------------- 55 * import static org.settings4j.ConnectorPositions.*; 56 * .. 57 * Connector myConnector = ... 58 * if (Settings4j.getSettings().getConnector(myConnector.getName()) == null) { 59 * Settings4j.getSettings().addConnector(myConnector, // 60 * firstValid(afterLast(SystemPropertyConnector.class), atFirst()) 61 * ); 62 * } 63 * -------------------------------------- 64 * </pre> 65 * 66 * @author brabenetz 67 */ 68 public final class ConnectorPositions { 69 70 /** Hide constructor (Utility Pattern). */ 71 private ConnectorPositions() { 72 super(); 73 } 74 75 /** 76 * @param connectorName The {@link Connector#getName()} to search for. 77 * @return {@link ConnectorPositionBeforeName} 78 */ 79 public static ConnectorPosition before(final String connectorName) { 80 return new ConnectorPositionBeforeName(connectorName); 81 } 82 83 /** 84 * @param connectorClass the connector class to search in the given connectors list. 85 * @return {@link ConnectorPositionBeforeFirstClass} 86 */ 87 public static ConnectorPosition beforeFirst(final Class<? extends Connector> connectorClass) { 88 return new ConnectorPositionBeforeFirstClass(connectorClass); 89 } 90 91 /** 92 * @param connectorClass the connector class to search in the given connectors list. 93 * @return {@link ConnectorPositionBeforeLastClass} 94 */ 95 public static ConnectorPosition beforeLast(final Class<? extends Connector> connectorClass) { 96 return new ConnectorPositionBeforeLastClass(connectorClass); 97 } 98 99 /** 100 * @param connectorName The {@link Connector#getName()} to search for. 101 * @return {@link ConnectorPositionAfterName} 102 */ 103 public static ConnectorPosition after(final String connectorName) { 104 return new ConnectorPositionAfterName(connectorName); 105 } 106 107 /** 108 * @param connectorClass the connector class to search in the given connectors list. 109 * @return {@link ConnectorPositionAfterFirstClass} 110 */ 111 public static ConnectorPosition afterFirst(final Class<? extends Connector> connectorClass) { 112 return new ConnectorPositionAfterFirstClass(connectorClass); 113 } 114 115 /** 116 * @param connectorClass the connector class to search in the given connectors list. 117 * @return {@link ConnectorPositionAfterLastClass} 118 */ 119 public static ConnectorPosition afterLast(final Class<? extends Connector> connectorClass) { 120 return new ConnectorPositionAfterLastClass(connectorClass); 121 } 122 123 /** 124 * @return {@link ConnectorPositionAtFirst} which always returns an valid position. 125 */ 126 public static ConnectorPosition atFirst() { 127 return new ConnectorPositionAtFirst(); 128 } 129 130 /** 131 * @return {@link ConnectorPositionAtLast} which always returns an valid position. 132 */ 133 public static ConnectorPosition atLast() { 134 return new ConnectorPositionAtLast(); 135 } 136 137 /** 138 * @param connectorPosition the {@link ConnectorPosition}s to search for a valid position. 139 * @return ConnectorPositionFirstValid 140 */ 141 public static ConnectorPosition firstValid(final ConnectorPosition... connectorPosition) { 142 return new ConnectorPositionFirstValid(connectorPosition); 143 } 144 }