Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

558321 design general purpose interfaces to empower reporting #81

Merged
merged 1 commit into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*******************************************************************************
* Copyright (c) 2019 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.yars.internal.api.pagination;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.eclipse.passage.loc.yars.internal.api.FetchedData;
import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage;
import org.eclipse.passage.loc.yars.internal.api.model.StoredEntry;

@SuppressWarnings("restriction")

class Fetch implements FetchedData<InMemoryStorage, StoredEntry> {

private final PaginationSettings settings;
private final InMemoryStorage storage;

Fetch(PaginationSettings settings, InMemoryStorage storage) {
this.settings = settings;
this.storage = storage;
}

@Override
public List<StoredEntry> get() {
List<StoredEntry> all = storage.entries();
return IntStream.range(settings.from(), settings.to()) //
.filter(i -> i >= 0) //
.filter(i -> i < all.size()) //
.mapToObj(all::get) //
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2019 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.yars.internal.api.pagination;

import org.eclipse.passage.loc.yars.internal.api.FetchedData;
import org.eclipse.passage.loc.yars.internal.api.Query;
import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage;
import org.eclipse.passage.loc.yars.internal.api.model.StoredEntry;

@SuppressWarnings("restriction")
public class Page implements Query<InMemoryStorage, StoredEntry, PaginationSettings> {

@Override
public String id() {
return "PAGE"; //$NON-NLS-1$
}

@Override
public String description() {
return "Fetch paginated original entries from in-memory storage"; //$NON-NLS-1$
}

@Override
public FetchedData<InMemoryStorage, StoredEntry> data(InMemoryStorage storage, PaginationSettings params) {
return new Fetch(params, storage);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2019 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.yars.internal.api.pagination;

import org.eclipse.passage.loc.yars.internal.api.FetchParams;

@SuppressWarnings("restriction")
class PaginationSettings implements FetchParams {

private final int no;
private final int length;

PaginationSettings(int no, int length) {
this.no = no;
this.length = length;
}

int from() {
return no * length;
}

int to() {
return (no + 1) * length;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2019 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.yars.internal.api.pagination;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage;
import org.eclipse.passage.loc.yars.internal.api.model.StoredEntry;
import org.junit.Test;

public class PaginationTest {

@Test
public void testFirstPage() {
List<StoredEntry> result = pageContent(0);
assertEquals(3, result.size());
assertTrue(result.stream().allMatch(e -> e.origin().equals("page 1"))); //$NON-NLS-1$
}

@Test
public void testMidPage() {
List<StoredEntry> result = pageContent(1);
assertEquals(3, result.size());
assertTrue(result.stream().allMatch(e -> e.origin().equals("page 2"))); //$NON-NLS-1$
}

@Test
public void testNotFullPage() {
List<StoredEntry> result = pageContent(2);
assertEquals(1, result.size());
assertEquals("page 3", result.get(0).origin()); //$NON-NLS-1$
}

@SuppressWarnings("restriction")
private List<StoredEntry> pageContent(int no) {
return new Page().data(base(), new PaginationSettings(no, 3)).get();
}

private InMemoryStorage base() {
return new InMemoryStorage( //
new StoredEntry("1.0", "page 1"), //$NON-NLS-1$ //$NON-NLS-2$
new StoredEntry("1.1", "page 1"), //$NON-NLS-1$ //$NON-NLS-2$
new StoredEntry("1.2", "page 1"), //$NON-NLS-1$ //$NON-NLS-2$
new StoredEntry("2.0", "page 2"), //$NON-NLS-1$ //$NON-NLS-2$
new StoredEntry("2.1", "page 2"), //$NON-NLS-1$ //$NON-NLS-2$
new StoredEntry("2.2", "page 2"), //$NON-NLS-1$ //$NON-NLS-2$
new StoredEntry("3.1", "page 3")//$NON-NLS-1$ //$NON-NLS-2$
);
}

}