Skip to content

Commit

Permalink
Add and throw custom invalid state error
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Oct 25, 2024
1 parent 1006aa3 commit 124b435
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/main/java/net/lostluma/battery/api/Battery.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.lostluma.battery.api;

import net.lostluma.battery.api.exception.InvalidStateError;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand Down Expand Up @@ -127,7 +128,7 @@ public interface Battery {
* Refresh battery information in-place.
*
* @throws IOException battery information couldn't be refreshed.
* @throws RuntimeException the associated manager is not active.
* @throws InvalidStateError the associated manager isn't active.
*/
void update() throws IOException, RuntimeException;
void update() throws IOException, InvalidStateError;
}
5 changes: 3 additions & 2 deletions src/main/java/net/lostluma/battery/api/Manager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.lostluma.battery.api;

import net.lostluma.battery.api.exception.InvalidStateError;
import net.lostluma.battery.api.exception.LibraryLoadError;
import net.lostluma.battery.impl.ManagerImpl;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -32,9 +33,9 @@ public interface Manager extends AutoCloseable {
*
* @return the system's current batteries.
* @throws IOException looking up the batteries failed.
* @throws RuntimeException the manager is already closed.
* @throws InvalidStateError the manager is already closed.
*/
@NotNull Collection<Battery> batteries() throws IOException, RuntimeException;
@NotNull Collection<Battery> batteries() throws IOException, InvalidStateError;

/**
* Close the manager.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.lostluma.battery.api.exception;

import org.jetbrains.annotations.ApiStatus;

/**
* Error thrown when calling a method on a closed object.
*/
// TODO (breaking): Subclass Error instead
public class InvalidStateError extends RuntimeException {
@ApiStatus.Internal
public InvalidStateError(String message) {
super(message);
}
}
5 changes: 3 additions & 2 deletions src/main/java/net/lostluma/battery/impl/BatteryImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.lostluma.battery.impl;

import net.lostluma.battery.api.exception.InvalidStateError;
import net.lostluma.battery.api.Battery;
import net.lostluma.battery.api.State;
import net.lostluma.battery.api.Technology;
Expand Down Expand Up @@ -49,11 +50,11 @@ private BatteryImpl(long ptr, ManagerImpl manager, Technology technology, @Nulla
}

@Override
public void update() throws IOException, RuntimeException {
public void update() throws IOException, InvalidStateError {
if (this.manager.isActive()) {
this.update0();
} else {
throw new RuntimeException("Attached manager is closed.");
throw new InvalidStateError("Attached manager is closed.");
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/lostluma/battery/impl/ManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.lostluma.battery.api.Battery;
import net.lostluma.battery.api.Manager;
import net.lostluma.battery.api.exception.InvalidStateError;
import net.lostluma.battery.api.exception.LibraryLoadError;
import net.lostluma.battery.impl.util.NativeUtil;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -41,7 +42,7 @@ boolean isActive() {
}

@Override
public @NotNull Collection<Battery> batteries() throws IOException, RuntimeException {
public @NotNull Collection<Battery> batteries() throws IOException, InvalidStateError {
if (this.isActive()) {
BatteryImpl[] batteries = this.batteries0();

Expand All @@ -57,7 +58,7 @@ boolean isActive() {

return Arrays.asList(batteries);
} else {
throw new RuntimeException("Manager can not be used after being closed!");
throw new InvalidStateError("Manager can not be used after being closed!");
}
}

Expand Down

0 comments on commit 124b435

Please sign in to comment.