Visitor { public boolean isHitRateBreached(Double maxHitRate, Duration maxHitRateDelay) { if (maxHitRate == null) return false; Duration age = getAge(); if (maxHitRateDelay != null && age.compareTo(maxHitRateDelay) <= 0) return false; return 1000.0 * hits / age.toMillis() > maxHitRate; } Visitor(long id, String ipAddress, String countryId, String country, String agentType, int hits, Instant firstHit, Instant lastHit); long getId(); String getIpAddress(); String getCountryId(); String getCountry(); String getAgentType(); synchronized int getHits(); Instant getFirstHit(); synchronized Instant getLastHit(); synchronized Duration getAge(); synchronized void visit(); synchronized void unvisit(); boolean isMaxHitsBreached(Integer maxHits); boolean isHitRateBreached(Double maxHitRate, Duration maxHitRateDelay); synchronized boolean isDirty(); synchronized void clearDirty(); synchronized boolean isExpired(Duration expiryTimeout); @Override String toString();  }
@Test void checkIfMaxHitRateIsBreached() { Instant now = Instant.now(); Visitor visitor = new Visitor(1L, "192.168.1.1", "SRB", "Serbia", WEB_BROWSER.name(), 200, now, now.plus(Duration.ofMinutes(2))); assertThat(visitor.isHitRateBreached(null, null)).isFalse(); assertThat(visitor.isHitRateBreached(1.0, null)).isTrue(); assertThat(visitor.isHitRateBreached(1.0, Duration.ofMinutes(5))).isFalse(); assertThat(visitor.isHitRateBreached(1.0, Duration.ofSeconds(1))).isTrue(); assertThat(visitor.isHitRateBreached(10.0, null)).isFalse(); assertThat(visitor.isHitRateBreached(10.0, Duration.ofSeconds(1))).isFalse(); }