Skip to content

Commit

Permalink
Allow HeapSize option to override other HeapSize options
Browse files Browse the repository at this point in the history
  • Loading branch information
thegridman committed Apr 25, 2023
1 parent 23b30c9 commit 1edf32f
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ public class HeapSize implements ComposableOption<HeapSize>, JvmOption
*/
private Units maximumUnits;

/**
* A flag that when {@code true} indicates this {@link HeapSize}
* option should override any other initial {@link HeapSize} options
* when being added to an {@link OptionsByType}.
*/
private boolean overrideInitial;

/**
* A flag that when {@code true} indicates this {@link HeapSize}
* option should override any other maximum {@link HeapSize} options
* when being added to an {@link OptionsByType}.
*/
private boolean overrideMaximum;

/**
* Privately constructs a default {@link HeapSize}.
Expand All @@ -82,10 +95,12 @@ private HeapSize()
*/
private HeapSize(HeapSize heapSize)
{
this.initial = heapSize.initial;
this.initialUnits = heapSize.initialUnits;
this.maximum = heapSize.maximum;
this.maximumUnits = heapSize.maximumUnits;
this.initial = heapSize.initial;
this.initialUnits = heapSize.initialUnits;
this.maximum = heapSize.maximum;
this.maximumUnits = heapSize.maximumUnits;
this.overrideInitial = heapSize.overrideInitial;
this.overrideMaximum = heapSize.overrideMaximum;
}


Expand Down Expand Up @@ -161,11 +176,30 @@ public static HeapSize useDefaults()
*/
public static HeapSize initial(int amount,
Units units)
{
return initial(amount, units, false);
}


/**
* Obtains a {@link HeapSize} with the specified initial amount
* (and a default maximum size)
*
* @param amount the initial {@link HeapSize}
* @param units the units of the initial {@link HeapSize}
* @param override this {@link HeapSize} overrides any other initial {@link HeapSize} options
*
* @return the {@link HeapSize}
*/
public static HeapSize initial(int amount,
Units units,
boolean override)
{
HeapSize heapSize = new HeapSize();

heapSize.initial = amount;
heapSize.initialUnits = units;
heapSize.initial = amount;
heapSize.initialUnits = units;
heapSize.overrideInitial = override;

return heapSize;
}
Expand All @@ -182,16 +216,33 @@ public static HeapSize initial(int amount,
*/
public static HeapSize maximum(int amount,
Units units)
{
return maximum(amount, units, false);
}

/**
* Obtains a {@link HeapSize} with the specified maximum amount
* (and a default initial size).
*
* @param amount the maximum {@link HeapSize}
* @param units the units of the maximum {@link HeapSize}
* @param override this {@link HeapSize} overrides any other maximum {@link HeapSize} options
*
* @return the {@link HeapSize}
*/
public static HeapSize maximum(int amount,
Units units,
boolean override)
{
HeapSize heapSize = new HeapSize();

heapSize.maximum = amount;
heapSize.maximumUnits = units;
heapSize.maximum = amount;
heapSize.maximumUnits = units;
heapSize.overrideMaximum = override;

return heapSize;
}


/**
* Obtains a {@link HeapSize} with the values.
*
Expand All @@ -206,14 +257,36 @@ public static HeapSize of(int initial,
Units initialUnits,
int maximum,
Units maximumUnits)
{
return of(initial, initialUnits, maximum, maximumUnits, false);
}

/**
* Obtains a {@link HeapSize} with the values.
*
* @param initial the initial {@link HeapSize}
* @param initialUnits the units of the initial {@link HeapSize}
* @param maximum the maximum {@link HeapSize}
* @param maximumUnits the units of the maximum {@link HeapSize}
* @param override this {@link HeapSize} overrides any other {@link HeapSize} options
*
* @return the {@link HeapSize}
*/
public static HeapSize of(int initial,
Units initialUnits,
int maximum,
Units maximumUnits,
boolean override)
{
HeapSize heapSize = new HeapSize();

heapSize.initial = initial;
heapSize.initialUnits = initialUnits;
heapSize.initial = initial;
heapSize.initialUnits = initialUnits;
heapSize.overrideInitial = override;

heapSize.maximum = maximum;
heapSize.maximumUnits = maximumUnits;
heapSize.maximum = maximum;
heapSize.maximumUnits = maximumUnits;
heapSize.overrideMaximum = override;

return heapSize;
}
Expand Down Expand Up @@ -272,29 +345,57 @@ public HeapSize compose(HeapSize other)
long initial = this.initial <= 0 ? 0 : this.getInitialSizeAs(Units.KB);
long otherInitial = other.initial <= 0 ? 0 : other.getInitialSizeAs(Units.KB);

if (initial > otherInitial)
if (other.overrideInitial)
{
result.initial = other.initial;
result.initialUnits = other.initialUnits;
result.overrideInitial = true;
}
else if (this.overrideInitial)
{
result.initial = this.initial;
result.initialUnits = this.initialUnits;
result.initial = this.initial;
result.initialUnits = this.initialUnits;
result.overrideInitial = true;
}
else if (initial > otherInitial)
{
result.initial = this.initial;
result.initialUnits = this.initialUnits;
result.overrideInitial = false;
}
else
{
result.initial = other.initial;
result.initialUnits = other.initialUnits;
result.initial = other.initial;
result.initialUnits = other.initialUnits;
result.overrideInitial = false;
}

long maximum = this.maximum <= 0 ? 0 : this.getMaximumSizeAs(Units.KB);
long otherMaximum = other.maximum <= 0 ? 0 : other.getMaximumSizeAs(Units.KB);

if (maximum > otherMaximum)
if (other.overrideMaximum)
{
result.maximum = other.maximum;
result.maximumUnits = other.maximumUnits;
result.overrideMaximum = true;
}
else if (this.overrideMaximum)
{
result.maximum = this.maximum;
result.maximumUnits = this.maximumUnits;
result.overrideMaximum = true;
}
else if (maximum > otherMaximum)
{
result.maximum = this.maximum;
result.maximumUnits = this.maximumUnits;
result.maximum = this.maximum;
result.maximumUnits = this.maximumUnits;
result.overrideMaximum = false;
}
else
{
result.maximum = other.maximum;
result.maximumUnits = other.maximumUnits;
result.maximum = other.maximum;
result.maximumUnits = other.maximumUnits;
result.overrideMaximum = false;
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,117 @@ public void shouldUseComposeSameMinimumAndMaximumHeapSizes()
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.KB), is(1048576L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.KB), is(1048576L));
}

/**
* Ensure that {@link HeapSize}s are composed when provided to {@link OptionsByType}.
*/
@Test
public void shouldComposeInitialHeapSizesWhenSecondHeapSizeIsOverride()
{
OptionsByType optionsByType = OptionsByType.of(HeapSize.initial(1, HeapSize.Units.GB));
optionsByType.add(HeapSize.initial(128, HeapSize.Units.MB, true));

HeapSize heapSize = optionsByType.get(HeapSize.class);

assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(128L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(0L));
}


/**
* Ensure that {@link HeapSize}s are composed when provided to {@link OptionsByType}.
*/
@Test
public void shouldComposeInitialHeapSizesWhenMultipleHeapSizeHaveOverride()
{
OptionsByType optionsByType = OptionsByType.of(HeapSize.initial(1, HeapSize.Units.GB));

HeapSize heapSize = optionsByType.get(HeapSize.class);
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.GB), is(1L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(0L));

optionsByType.add(HeapSize.initial(3, HeapSize.Units.MB));
heapSize = optionsByType.get(HeapSize.class);
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.GB), is(1L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(0L));

optionsByType.add(HeapSize.initial(3, HeapSize.Units.MB, true));
heapSize = optionsByType.get(HeapSize.class);
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(3L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(0L));

optionsByType.add(HeapSize.initial(10, HeapSize.Units.MB, false));
heapSize = optionsByType.get(HeapSize.class);
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(3L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(0L));

optionsByType.add(HeapSize.initial(1, HeapSize.Units.MB, true));
heapSize = optionsByType.get(HeapSize.class);
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(1L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(0L));
}

/**
* Ensure that {@link HeapSize}s are composed when provided to {@link OptionsByType}.
*/
@Test
public void shouldComposeMaximumHeapSizesWhenSecondHeapSizeIsOverride()
{
OptionsByType optionsByType = OptionsByType.of(HeapSize.maximum(1, HeapSize.Units.GB));
optionsByType.add(HeapSize.maximum(128, HeapSize.Units.MB, true));

HeapSize heapSize = optionsByType.get(HeapSize.class);

assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(0L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(128L));
}

/**
* Ensure that {@link HeapSize}s are composed when provided to {@link OptionsByType}.
*/
@Test
public void shouldComposeMaximumHeapSizesWhenMultipleHeapSizeHaveOverride()
{
OptionsByType optionsByType = OptionsByType.of(HeapSize.maximum(1, HeapSize.Units.GB));

HeapSize heapSize = optionsByType.get(HeapSize.class);
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(0L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.GB), is(1L));

optionsByType.add(HeapSize.maximum(3, HeapSize.Units.MB));
heapSize = optionsByType.get(HeapSize.class);
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(0L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.GB), is(1L));

optionsByType.add(HeapSize.maximum(3, HeapSize.Units.MB, true));
heapSize = optionsByType.get(HeapSize.class);
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(0L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(3L));

optionsByType.add(HeapSize.maximum(10, HeapSize.Units.MB, false));
heapSize = optionsByType.get(HeapSize.class);
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(0L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(3L));

optionsByType.add(HeapSize.maximum(1, HeapSize.Units.MB, true));
heapSize = optionsByType.get(HeapSize.class);
assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(0L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(1L));
}

/**
* Ensure that {@link HeapSize}s are composed when provided to {@link OptionsByType}.
*/
@Test
public void shouldComposeHeapSizesWhenSecondHeapSizeIsOverride()
{
OptionsByType optionsByType = OptionsByType.of(HeapSize.of(1, HeapSize.Units.GB, 5, HeapSize.Units.GB));
optionsByType.add(HeapSize.of(128, HeapSize.Units.MB, 256, HeapSize.Units.MB, true));

HeapSize heapSize = optionsByType.get(HeapSize.class);

assertThat(heapSize.getInitialSizeAs(HeapSize.Units.MB), is(128L));
assertThat(heapSize.getMaximumSizeAs(HeapSize.Units.MB), is(256L));
}

}

0 comments on commit 1edf32f

Please sign in to comment.