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