001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015package org.basepom.mojo.propertyhelper.definitions; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018import static com.google.common.base.Preconditions.checkState; 019 020import org.basepom.mojo.propertyhelper.FieldContext; 021import org.basepom.mojo.propertyhelper.ValueCache; 022import org.basepom.mojo.propertyhelper.ValueProvider; 023import org.basepom.mojo.propertyhelper.fields.NumberField; 024 025import java.io.IOException; 026import java.util.Objects; 027import java.util.Optional; 028import java.util.StringJoiner; 029 030import com.google.common.annotations.VisibleForTesting; 031 032public class NumberDefinition extends FieldDefinition<String> { 033 034 public static final String INITIAL_VALUE = "0"; 035 036 /** 037 * If a multi-number, which field to increment. Field injected by Maven. 038 */ 039 Integer fieldNumber; 040 041 /** 042 * Increment of the property when changing it. Field injected by Maven. 043 */ 044 int increment = 1; 045 046 public NumberDefinition() { 047 initialValue = INITIAL_VALUE; 048 } 049 050 @VisibleForTesting 051 NumberDefinition(String id) { 052 super(id); 053 initialValue = INITIAL_VALUE; 054 } 055 056 057 public Optional<Integer> getFieldNumber() { 058 return Optional.ofNullable(fieldNumber); 059 } 060 061 public int getIncrement() { 062 return increment; 063 } 064 065 @Override 066 public void check() { 067 super.check(); 068 checkState(getInitialValue().isPresent(), "initial value must be defined"); 069 getFieldNumber().ifPresent(fieldNumber -> checkState(fieldNumber >= 0, "the field number must be >= 0")); 070 } 071 072 @Override 073 public NumberField createField(FieldContext context, ValueCache valueCache) throws IOException { 074 checkNotNull(context, "context is null"); 075 checkNotNull(valueCache, "valueCache is null"); 076 077 check(); 078 079 final ValueProvider numberValue = valueCache.getValueProvider(this); 080 return new NumberField(this, numberValue, context); 081 } 082 083 @Override 084 public String toString() { 085 return new StringJoiner(", ", NumberDefinition.class.getSimpleName() + "[", "]") 086 .add("fieldNumber=" + fieldNumber) 087 .add("increment=" + increment) 088 .add(super.toString()) 089 .toString(); 090 } 091 092 @Override 093 public boolean equals(Object o) { 094 if (this == o) { 095 return true; 096 } 097 if (o == null || getClass() != o.getClass()) { 098 return false; 099 } 100 if (!super.equals(o)) { 101 return false; 102 } 103 NumberDefinition that = (NumberDefinition) o; 104 return increment == that.increment && Objects.equals(fieldNumber, that.fieldNumber); 105 } 106 107 @Override 108 public int hashCode() { 109 return Objects.hash(super.hashCode(), fieldNumber, increment); 110 } 111}