/** * Default constructor. * * @param referent * Object to refer to. * @param q * Reference queue to register Weak reference with. * @see WeakReference#WeakReference(Object, ReferenceQueue) */ public CustomWeakReference(T referent, ReferenceQueue<? super T> q) { super(referent, q); referentId = referent.getId(); }
/** * {@inheritDoc} */ @Override public List<DefaultData> findByExampleSinceIdIgnoreMethodId(DefaultData template) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<DefaultData> criteria = builder.createQuery(DefaultData.class); Root<? extends DefaultData> root = criteria.from(template.getClass()); criteria.select(root); Predicate id = builder.greaterThan(root.<Long> get("id"), template.getId()); Predicate platformId = builder.equal(root.get("platformIdent"), template.getPlatformIdent()); criteria.where(id, platformId); return entityManager.createQuery(criteria).getResultList(); }
/** * {@inheritDoc} */ @Override public E get(E template) { long id = template.getId(); WeakReference<E> weakReference = map.get(id); if (null != weakReference) { if (null == weakReference.get()) { map.remove(id); return null; } return weakReference.get(); } else { return null; } }
/** * {@inheritDoc} */ @Override public E put(E element) { CustomWeakReference<E> weakReference = new CustomWeakReference<>(element, referenceQueue); map.put(element.getId(), weakReference); return element; }
/** * {@inheritDoc} */ @Override public E getAndRemove(E template) { long id = template.getId(); WeakReference<E> weakReference = map.get(id); if (null != weakReference) { if (null == weakReference.get()) { map.remove(id); return null; } else { E result = weakReference.get(); map.remove(id); return result; } } else { return null; } }
/** * {@inheritDoc} */ @Override public List<DefaultData> getDataObjectsSinceIdIgnoreMethodId(DefaultData template) { StorageIndexQuery query = storageIndexQueryProvider.createNewStorageIndexQuery(); query.setMinId(template.getId()); ArrayList<Class<?>> searchClasses = new ArrayList<>(); searchClasses.add(template.getClass()); query.setObjectClasses(searchClasses); query.setPlatformIdent(template.getPlatformIdent()); return super.executeQuery(query); }
/** * Get and getAndRemove on empty leaf should return <code>null</code>. */ @Test public void getOnEmpyLeaf() { DefaultData element = mock(DefaultData.class); when(element.getId()).thenReturn(10L); assertThat(arrayBasedStorageLeaf.get(element), is(nullValue())); assertThat(arrayBasedStorageLeaf.getAndRemove(element), is(nullValue())); }
/** * {@inheritDoc} */ @Override public IStorageDescriptor put(E element) throws IndexingException { if (null == element) { throw new IndexingException("Element to index can not be null."); } else if (0 == element.getId()) { throw new IndexingException("Element to index can not have ID that is equal to zero."); } StorageDescriptor descriptor = new StorageDescriptor(this.id); if (insertIntoArrays(element.getId(), descriptor.getSimpleStorageDescriptor())) { return descriptor; } else { throw new IndexingException("Element already indexed."); } }
/** * Indexing same element twice should raise an exception. * * @throws IndexingException */ @Test(expectedExceptions = { IndexingException.class }) public void sameElementIndexedTwice() throws IndexingException { DefaultData element = mock(DefaultData.class); when(element.getId()).thenReturn(10L); arrayBasedStorageLeaf.put(element); arrayBasedStorageLeaf.put(element); }
/** * Adds wanted amount of elements to the leaf. * * @param treeComponent * {@link IStorageTreeComponent}. * @param amount * Amount of elements to add. * @throws IndexingException */ private void addElements(IStorageTreeComponent<DefaultData> treeComponent, int amount) throws IndexingException { DefaultData element = mock(DefaultData.class); for (int i = 1; i <= amount; i++) { when(element.getId()).thenReturn((long) i); treeComponent.put(element); } } }
/** * Test that the get on the {@link LeafWithNoDescriptors} will throw an exception if invoked. * * @throws IndexingException * If {@link IndexingException} occurs. */ @Test(expectedExceptions = { UnsupportedOperationException.class }) public void putAndGetElement() throws IndexingException { IStorageTreeComponent<DefaultData> rootBranch = new LeafWithNoDescriptors<>(); DefaultData defaultData = mock(DefaultData.class); when(defaultData.getId()).thenReturn(1L); rootBranch.put(defaultData); rootBranch.get(defaultData); }
/** * Tests that the indexing the same element in the {@link ArrayBasedStorageLeaf} will thrown a * exception. * * @throws IndexingException * {@link IndexingException} */ @Test(expectedExceptions = { IndexingException.class }) public void indexSameElement() throws IndexingException { ArrayBasedStorageLeaf<DefaultData> arrayBasedStorageLeaf = new ArrayBasedStorageLeaf<>(); DefaultData defaultData = mock(DefaultData.class); when(defaultData.getId()).thenReturn(1L); arrayBasedStorageLeaf.put(defaultData); arrayBasedStorageLeaf.put(defaultData); }
/** * Test the removing of the element in the {@link ArrayBasedStorageLeaf}. * * @throws IndexingException * {@link IndexingException} */ @Test public void removeElement() throws IndexingException { ArrayBasedStorageLeaf<DefaultData> arrayBasedStorageLeaf = new ArrayBasedStorageLeaf<>(); DefaultData defaultData = mock(DefaultData.class); when(defaultData.getId()).thenReturn(1L); arrayBasedStorageLeaf.put(defaultData); assertThat(arrayBasedStorageLeaf.getAndRemove(defaultData), is(notNullValue())); assertThat(arrayBasedStorageLeaf.get(defaultData), is(nullValue())); assertThat(arrayBasedStorageLeaf.query(storageIndexQuery), is(empty())); // Test for forkJoinPool assertThat(arrayBasedStorageLeaf.query(storageIndexQuery, forkJoinPool), is(empty())); }
/** * Tests the ID aggregation with in {@link IAggregatedData}. * * @param aggregated * {@link IAggregatedData} * @param data * Data to be aggregated. */ @SuppressWarnings("unchecked") @Test(dataProvider = "normalDataProvider") public <E extends DefaultData> void idAggregation(IIdsAwareAggregatedData<E> aggregated, E data) { aggregated.aggregate(data); if (data instanceof IIdsAwareAggregatedData) { for (long id : ((IIdsAwareAggregatedData<E>) data).getAggregatedIds()) { assertThat(aggregated.getAggregatedIds(), hasItem(id)); } } else { assertThat(aggregated.getAggregatedIds(), hasItem(data.getId())); } }
/** * {@inheritDoc} */ @Override public List<DefaultData> getDataObjectsSinceId(DefaultData template) { StorageIndexQuery query = storageIndexQueryProvider.createNewStorageIndexQuery(); query.setMinId(template.getId()); ArrayList<Class<?>> searchClasses = new ArrayList<>(); searchClasses.add(template.getClass()); query.setObjectClasses(searchClasses); query.setPlatformIdent(template.getPlatformIdent()); query.setSensorTypeIdent(template.getSensorTypeIdent()); if (template instanceof MethodSensorData) { query.setMethodIdent(((MethodSensorData) template).getMethodIdent()); if (template instanceof InvocationSequenceData) { query.setOnlyInvocationsWithoutChildren(true); } } return super.executeQuery(query); }
/** * Confirm {@link IndexingException} will be reaised when key can not be generated for element. * * @throws IndexingException */ @Test(expectedExceptions = { IndexingException.class }) public void putWithNoKey() throws IndexingException { IBufferTreeComponent<DefaultData> rootBranch = new Branch<>(new BufferBranchIndexer<>(new TimestampIndexer<>())); DefaultData defaultData = mock(DefaultData.class); when(defaultData.getId()).thenReturn(1L); rootBranch.put(defaultData); }
/** * Test put and retrieval of one element. * * @throws IndexingException * If {@link IndexingException} occurs. */ @Test public void putAndGetElement() throws IndexingException { IBufferTreeComponent<DefaultData> rootBranch = new Branch<>(new BufferBranchIndexer<>(new ObjectTypeIndexer<>())); DefaultData defaultData = mock(DefaultData.class); when(defaultData.getId()).thenReturn(1L); rootBranch.put(defaultData); assertThat(rootBranch.get(defaultData), is(equalTo(defaultData))); }
/** * Clear all test. * * @throws IndexingException * If {@link IndexingException} occurs. */ @Test public void clearBranch() throws IndexingException { IBufferTreeComponent<DefaultData> rootBranch = new Branch<>(new BufferBranchIndexer<>(new ObjectTypeIndexer<>())); DefaultData defaultData = mock(DefaultData.class); when(defaultData.getId()).thenReturn(1L); rootBranch.put(defaultData); rootBranch.clearAll(); assertThat(rootBranch.getNumberOfElements(), is(equalTo(0L))); }
/** * Test a removal of one element from the indexing tree. * * @throws IndexingException * If {@link IndexingException} occurs. */ @Test public void removeElement() throws IndexingException { IBufferTreeComponent<DefaultData> rootBranch = new Branch<>(new BufferBranchIndexer<>(new BufferBranchIndexer<>(new ObjectTypeIndexer<>()))); DefaultData defaultData = mock(DefaultData.class); when(defaultData.getId()).thenReturn(1L); rootBranch.put(defaultData); rootBranch.getAndRemove(defaultData); assertThat(rootBranch.get(defaultData), is(nullValue())); }