Skip to content

Commit

Permalink
<feat>(moon): Percent Strategy Support scale total percent.
Browse files Browse the repository at this point in the history
  • Loading branch information
yizzuide committed Mar 17, 2020
1 parent b047ca1 commit ef6fd3d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Milkomeda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<properties>
<java.version>1.8</java.version>
<project.release.version>2.7.2-SNAPSHOT</project.release.version>
<project.release.version>2.7.3-SNAPSHOT</project.release.version>
<spring-boot.version>2.2.4</spring-boot.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<mybatis.starter.version>2.1.1</mybatis.starter.version>
Expand Down Expand Up @@ -65,7 +65,7 @@
<profile>
<id>sonatype-oss-release</id>
<properties>
<project.release.version>2.7.2</project.release.version>
<project.release.version>2.7.3</project.release.version>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.yizzuide.milkomeda.moon;

import lombok.Data;

/**
* PercentMoonStrategy
* 百分比分配 <br>
Expand All @@ -10,14 +12,19 @@
*
* @author yizzuide
* @since 2.6.0
* @version 2.6.1
* @version 2.7.3
* Create at 2020/03/13 21:42
*/
@Data
public class PercentMoonStrategy implements MoonStrategy {
/**
* 总占百分
* 默认总占百分
*/
public static final int DEFAULT_PERCENT = 100;
/**
* 百分比
*/
public static final int PERCENT = 100;
public int percent = DEFAULT_PERCENT;

@Override
public <T> T getCurrentPhase(Moon<T> moon) {
Expand Down Expand Up @@ -48,23 +55,49 @@ public <T> T getPhase(String key, Integer p, Moon<T> prototype) {
@Override
public LeftHandPointer pluck(Moon<?> moon, LeftHandPointer leftHandPointer) {
int p = leftHandPointer.getCurrent();
p = (p + 1) % PERCENT;
p = (p + 1) % this.getPercent();
leftHandPointer.setCurrent(p);
return leftHandPointer;
}

/**
* 百分比表达式解析, 如:5/5、3/7、25/75
* 百分比表达式解析, 固定百分总值为100。如:5/5、3/7、25/75
* @param percentExpress 百分比表达式
* @return 百分比列表
*/
public static Integer[] parse(String percentExpress) {
return parse(percentExpress, null);
}

/**
* 百分比表达式解析, 并自动缩放百分总值 <br>
* <pre>
* 5/5、3/7:百分总值为10
* 25/75:百分总值为100
* </pre>
* @param percentExpress 百分比表达式
* @param strategy PercentMoonStrategy
* @return 百分比列表
*/
public static Integer[] parse(String percentExpress, MoonStrategy strategy) {
String[] percentComps = percentExpress.split("/");
if (percentComps.length < 2) {
throw new IllegalArgumentException("Percent express format is illegal: " + percentExpress);
}
Integer[] percentArray = new Integer[percentComps.length];
for (int i = 0; i < percentComps.length; i++) {
String percentComp = percentComps[i];
if (percentComp.length() > 2 || percentComp.startsWith("0")) {
throw new IllegalArgumentException("Percent express format is illegal: " + percentExpress);
}
if (percentComp.length() == 1) {
percentComps[i] = percentComp + "0";
if (strategy == null) {
// 补充到百分的比例
percentComps[i] = percentComp + "0";
} else if (strategy instanceof PercentMoonStrategy){
// 缩小百分分配总值
((PercentMoonStrategy) strategy).setPercent(10);
}
}
percentArray[i] = Integer.valueOf(percentComps[i]);
}
Expand Down
2 changes: 1 addition & 1 deletion MilkomedaDemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<milkomeda.version>2.7.2-SNAPSHOT</milkomeda.version>
<milkomeda.version>2.7.3-SNAPSHOT</milkomeda.version>
<mybatis.starter>2.1.1</mybatis.starter>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Moon<Integer> abTestMoon() {
// AB测试阶段值:15%为0,85%为1
// moon.add(15, 85);
// moon.add(PercentMoonStrategy.parse("15/85"));
moon.add(PercentMoonStrategy.parse("1/9"));
moon.add(PercentMoonStrategy.parse("3/7", moon.getMoonStrategy()));
return moon;
}
}

0 comments on commit ef6fd3d

Please sign in to comment.