Skip to content

Using Stonks Core

Getting Stonks Core library

Stonks Core is available through JitPack:

repositories {
    maven { url = 'https://jitpack.io/' } 
}

dependencies {
    implementation 'com.github.nahkd123.stonks:stonks-core:main-SNAPSHOT' 
}
<repositories>
    <repository> 
        <id>JitPack</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.github.nahkd123.stonks</groupId>
        <artifactId>stonks-core</artifactId>
        <version>main-SNAPSHOT</version> 
    </dependency>
</dependencies>

Using Stonks Core in your project

Creating service

Stonks Core provides StonksMemoryService, a service that stores everything inside memory. For a small project, this is sufficient enough.

import stonks.core.service.memory.*;

StonksMemoryService service = new StonksMemoryService();

// Adding category
// Products information are provided by services so front-ends doesn't have to
// rely on configurations to know all products, thanks to construction data.
MemoryCategory category = new MemoryCategory("category_id", "My Category");
service.getModifiableCategories().add(category);

// Adding product
String constructionData = "Construction data here..."; 
MemoryProduct product = new MemoryProduct(category, "product_id", "My Product", constructionData);
category.getModifiableMockProducts().add(product);

Making service calls

In production environment, service calls takes time to get processed, so all methods in StonksService return Task<?>, which are Nahara's Toolkit Tasks:

public interface StonksService {
    public Task<List<Category>> queryAllCategories();
    public Task<ProductMarketOverview> queryProductMarketOverview(Product product);
    public Task<List<Offer>> getOffers(UUID offerer);
    public Task<Offer> claimOffer(Offer offer);
    public Task<Offer> cancelOffer(Offer offer);
    public Task<Offer> listOffer(UUID offerer, Product product, OfferType type, int units, double pricePerUnit);
    public Task<InstantOfferExecuteResult> instantOffer(Product product, OfferType type, int units, double balance);
    
}

Since we are not in production environment, we want to obtain result without writing extra code just to deal with potential errors/wait time. For that, you can use .await():

List<Category> categories = service.queryAllCategories().await();
Product product = categories.stream()
    .filter(p -> p.getProductId().equals("product_id"))
    .findFirst().get();
ProductMarketOverview overview = service.queryProductMarketOverview(product).await();
OverviewOffersList buyOffers = overview.getBuyOffers();
// ...

Custom service implementation

Implement your own service by implementing StonksService interface. You can take a look at source code for StonksMemoryService if you need example.