litesoft
@ 480
litesoft / trunk / GWT_Sandbox / Prioritizer / src / org / litesoft / prioritizer / client / support / PriorityGroup.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
package org.litesoft.prioritizer.client.support; public enum PriorityGroup { Initial( 0 ), Low( 2000, 0, false ), MediumLow( 4000, 2000 ), Medium( 6000, 4000 ), MediumHigh( 8000, 6000 ), High( 9990, 8000 ), Critical( 10000, 9990 ); public static final float TOP_EXCLUSIVE_VALUE = 10000f; public static final float BOTTOM_INCLUSIVE_VALUE = 0f; private float mRangeTop; private boolean mTopInclusive; private float mRangeBottom; private boolean mBottomInclusive; public PriorityGroup higherGroup() { return (Critical == this) ? this : values()[ordinal() + 1]; } public static PriorityGroup from( Float pValue ) { if ( (pValue == null) || (pValue < BOTTOM_INCLUSIVE_VALUE) ) { return Initial; } for ( PriorityGroup zValue : values() ) { if ( zValue.isInRange( pValue ) ) { return zValue; } } return Critical; } PriorityGroup( float pRangeTop, boolean pTopInclusive, float pRangeBottom, boolean pBottomInclusive ) { mRangeTop = pRangeTop; mTopInclusive = pTopInclusive; mRangeBottom = pRangeBottom; mBottomInclusive = pBottomInclusive; } PriorityGroup( float pRangeTop, float pRangeBottom, boolean pBottomInclusive ) { this( pRangeTop, false, pRangeBottom, pBottomInclusive ); } PriorityGroup( float pRangeTop, float pRangeBottom ) { this( pRangeTop, pRangeBottom, true ); } PriorityGroup( float pRangeValue ) { this( pRangeValue, true, pRangeValue, true ); } public float getRangeTop() { return mRangeTop; } public boolean isTopInclusive() { return mTopInclusive; } public float getRangeBottom() { return mRangeBottom; } public boolean isBottomInclusive() { return mBottomInclusive; } public float getMidRange() { return (getRangeTop() + getRangeBottom()) / 2; } public boolean isInRange( float pValue ) { return isNotTooSmall( pValue ) && isNotTooLarge( pValue ); } public Range getRangeAssumingExclusive() { return new Range( getRangeBottom(), getRangeTop() ); } public static PriorityGroup deNull( PriorityGroup pGroup, PriorityGroup pDefault ) { return (pGroup != null) ? pGroup : pDefault; } /** * @param pValues - are !null & are NOT to be decending in value between any two sequential pairs */ public static float[] rePrioritizeBetween( Float pBottomExclusive, Float pTopExclusive, Float... pValues ) { return new Range( pBottomExclusive, pTopExclusive ).update().rePrioritizeBetween( pValues ); } private boolean isNotTooLarge( float pValue ) { return mTopInclusive ? (pValue <= mRangeTop) : (pValue < mRangeTop); } private boolean isNotTooSmall( float pValue ) { return mBottomInclusive ? (mRangeBottom <= pValue) : (mRangeBottom < pValue); } public static class Range { private float mBottomExclusive, mTopExclusive; public Range( Float pBottomExclusive, Float pTopExclusive ) { mBottomExclusive = normalize( pBottomExclusive, BOTTOM_INCLUSIVE_VALUE ); mTopExclusive = normalize( pTopExclusive, TOP_EXCLUSIVE_VALUE ); if ( mTopExclusive <= mBottomExclusive ) { throw new IllegalArgumentException( "Bottom (" + mBottomExclusive + ") must be less than Top (" + mTopExclusive + ")" ); } } private static float normalize( Float pValue, float pDefault ) { return (pValue != null) && (BOTTOM_INCLUSIVE_VALUE <= pValue) && (pValue < TOP_EXCLUSIVE_VALUE) ? pValue : pDefault; } public float getTopExclusive() { return mTopExclusive; } public float getBottomExclusive() { return mBottomExclusive; } public Range update() { PriorityGroup zGroup = from( mBottomExclusive ); if ( mBottomExclusive == zGroup.getRangeTop() ) { zGroup = zGroup.higherGroup(); mBottomExclusive = zGroup.getRangeBottom(); } if ( zGroup != from( mTopExclusive ) ) { mTopExclusive = zGroup.getRangeTop(); } return this; } /** * @param pValues - are !null & are NOT to be decending in value between any two sequential pairs */ public float[] rePrioritizeBetween( Float... pValues ) { int zValuesLength = (pValues == null) ? 0 : pValues.length; float[] rvs = new float[zValuesLength]; float zDelta = (mTopExclusive - mBottomExclusive) / (zValuesLength + 1); float zRunningValue = mTopExclusive; for ( int i = 0; i < zValuesLength; i++ ) { rvs[i] = zRunningValue -= zDelta; } return rvs; } } } |