Skip to content

Commit

Permalink
Fix hashcode implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Aug 14, 2023
1 parent 7b1d0c9 commit 47bf2c7
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 41 deletions.
11 changes: 6 additions & 5 deletions packages/cli/lib/behavior/behavior.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ class JobException implements Exception {
explicitBehavior == other.explicitBehavior;

@override
int get hashCode =>
message.hashCode ^
timeout.hashCode ^
disable.hashCode ^
explicitBehavior.hashCode;
int get hashCode => Object.hash(
message,
timeout,
disable,
explicitBehavior,
);
}

/// Exception thrown from a Job if the condition is not met.
Expand Down
11 changes: 6 additions & 5 deletions packages/cli/lib/cache/shipyard_prices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ class ShipyardPrice {
timestamp == other.timestamp;

@override
int get hashCode =>
waypointSymbol.hashCode ^
shipType.hashCode ^
purchasePrice.hashCode ^
timestamp.hashCode;
int get hashCode => Object.hash(
waypointSymbol,
shipType,
purchasePrice,
timestamp,
);
}

/// A collection of price records.
Expand Down
2 changes: 1 addition & 1 deletion packages/types/lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class ShipSymbol implements Comparable<ShipSymbol> {
number == other.number;

@override
int get hashCode => agentName.hashCode ^ number.hashCode;
int get hashCode => Object.hash(agentName, number);
}

/// Extensions onto System to make it easier to work with.
Expand Down
22 changes: 12 additions & 10 deletions packages/types/lib/deal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ class ContractDelivery {
maxUnits == other.maxUnits;

@override
int get hashCode =>
contractId.hashCode ^
destination.hashCode ^
tradeSymbol.hashCode ^
rewardPerUnit.hashCode ^
maxUnits.hashCode;
int get hashCode => Object.hash(
contractId,
destination,
tradeSymbol,
rewardPerUnit,
maxUnits,
);
}

/// Record of a possible arbitrage opportunity.
Expand Down Expand Up @@ -236,10 +237,11 @@ class Deal {
contractDelivery == other.contractDelivery;

@override
int get hashCode =>
sourcePrice.hashCode ^
destinationPrice.hashCode ^
contractDelivery.hashCode;
int get hashCode => Object.hash(
sourcePrice,
destinationPrice,
contractDelivery,
);
}

/// A deal between two markets which considers flight cost and time.
Expand Down
17 changes: 9 additions & 8 deletions packages/types/lib/market_price.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ class MarketPrice {
timestamp == other.timestamp;

@override
int get hashCode =>
waypointSymbol.hashCode ^
symbol.hashCode ^
supply.hashCode ^
purchasePrice.hashCode ^
sellPrice.hashCode ^
tradeVolume.hashCode ^
timestamp.hashCode;
int get hashCode => Object.hash(
waypointSymbol,
symbol,
supply,
purchasePrice,
sellPrice,
tradeVolume,
timestamp,
);
}
25 changes: 13 additions & 12 deletions packages/types/lib/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,17 @@ class Transaction {
accounting == other.accounting;

@override
int get hashCode =>
transactionType.hashCode ^
shipSymbol.hashCode ^
waypointSymbol.hashCode ^
tradeSymbol.hashCode ^
shipType.hashCode ^
quantity.hashCode ^
tradeType.hashCode ^
perUnitPrice.hashCode ^
timestamp.hashCode ^
agentCredits.hashCode ^
accounting.hashCode;
int get hashCode => Object.hash(
transactionType,
shipSymbol,
waypointSymbol,
tradeSymbol,
shipType,
quantity,
tradeType,
perUnitPrice,
timestamp,
agentCredits,
accounting,
);
}
36 changes: 36 additions & 0 deletions packages/types/test/transaction_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,40 @@ void main() {
expect(json2, json);
expect(transaction2, transaction);
});

// We don't actually care that hashCode is correct, we just implement it
// because we implement equals. But it was wrong at one point, so we test
// that it's not wrong anymore.
test('Transaction.hashCode', () {
final moonLanding = DateTime.utc(1969, 7, 20, 20, 18, 04);

final transaction = Transaction(
transactionType: TransactionType.market,
waypointSymbol: WaypointSymbol.fromString('S-E-P'),
shipSymbol: const ShipSymbol('S', 1),
tradeSymbol: TradeSymbol.FUEL,
shipType: null,
quantity: 1,
tradeType: MarketTransactionTypeEnum.PURCHASE,
perUnitPrice: 2,
timestamp: moonLanding,
agentCredits: 3,
accounting: AccountingType.capital,
);
final transaction2 = Transaction(
transactionType: TransactionType.market,
waypointSymbol: WaypointSymbol.fromString('S-E-P'),
shipSymbol: const ShipSymbol('S', 1),
tradeSymbol: TradeSymbol.FUEL,
shipType: null,
quantity: 3,
tradeType: MarketTransactionTypeEnum.PURCHASE,
perUnitPrice: 2,
timestamp: moonLanding,
agentCredits: 1,
accounting: AccountingType.capital,
);
// Note: Quanity and agentCredits are swapped relative to transaction1:
expect(transaction.hashCode, isNot(transaction2.hashCode));
});
}

0 comments on commit 47bf2c7

Please sign in to comment.