Skip to content

Instantly share code, notes, and snippets.

/*
This is using single Int to manage 32 locks in thread safe way.
This has less memory usage as compared to JDK lock which uses one Int(32 Bytes) to manage single lock.
*/
public class Locks {
public static final int INT_BYTES = 32;
private AtomicInteger lock = new AtomicInteger(0);
public boolean lock(int index) {
/*
Using bits count to find distance between 2 integer. Some of application are error deduction while data transfer
*/
public class HammingDistance {
public static int weight(int value) {
return Bits.countBits(value);
}
public static int distance(int value1, int value2) {
/*
Used for verification for data transferred over network or data saved on disk. Parity bits is used in many hardware for deducting errors.
Caution: This is simple technique and comes with some limitation of deduction of error with odd or even.
Hadoop name nodes performs some checks like this to check data integrity.
*/
public class Transmission {
public static byte transmit(byte data) {
return Bits.oddParity(data); // Add 1 bit to keep odd parity if required.
}
public class CustomerActivity {
private final int[] months = new int[12];
public void record(LocalDate day) {
int monthOffSet = day.getMonthValue() - 1;
int monthValue = months[monthOffSet];
// Set bit for day in 32 bit int and then OR(|) with month value to merge value
months[monthOffSet] = monthValue | 1 << (day.getDayOfMonth() - 1);
}
case class Registration(pk1: String, pk2: String,
className: String = null, classDesc: String = null, frequency: Int = 0,
firstName: String = null, lastName: String = null)
Registration("class:C1", "class:C1", className = "Maths", classDesc = "Basic Algebra")
Registration("class:C1", "student:S1", firstName = "Student1", lastName = "Student1")
Registration("class:C1", "student:S2", firstName = "Student2", lastName = "Student2")
Registration("class:C1", "student:S2", firstName = "Student2", lastName = "Student2")
case class Product(id: Long, name: String, desc: String, brand: String, price: Double)
case class OrderItem(orderId: Long, itemId: Long, noOfUnit: Int, totalPrice: Double,
productName: String, productDesc: String, productBrand: String, productPrice: Double)
case class Customer(customerId: String, email: String, paymentOption: List[PaymentOption])
case class InternetBanking(bankName: String, userId: String, bankWebSite: URI) extends PaymentOption
case class CreditCard(cardType: String, issuer: String, cardNo: Long) extends PaymentOption
case class DebitCard(cardType: String, issuer: String, cardNo: Long) extends PaymentOption
case class Payla(id: String) extends PaymentOption
@RegisterExtension
JUnit5Mockery context = new JUnit5Mockery();
context.checking(new Expectations() {{
oneOf(currencyConverter).convert(1, "SGD", "INR");
will(returnValue(50d));
oneOf(bankService).deposit(100d, account);
will(returnValue("99999"));
}});
expect(currencyConverter.convert(1, "SGD", "INR")).andReturn(50d);
expect(bankService.deposit(100d, account)).andReturn("99999");
replay(currencyConverter, bankService);
@Test
public void transfer_sgd_to_inr() {
FXService fxService = new FXService(currencyConverter, bankService, 0.0d);
BankAccount account = new BankAccount("1111-22222", "SuperStableBank");
expect(currencyConverter.convert(1, "SGD", "INR")).andReturn(50d);
expect(bankService.deposit(100d, account)).andReturn("99999");
replay(currencyConverter, bankService);