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

Bump actions/checkout from 3 to 4 #79

Closed
wants to merge 21 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Configuration file of Github Dependabot
# Configuration file of GitHub Dependabot

version: 2
updates:
Expand Down
49 changes: 18 additions & 31 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Workflow file of Github Actions
# Workflow file of GitHub Actions

name: build

Expand All @@ -12,44 +12,31 @@ on:
- master

jobs:
build-on-jdk-8:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout scm
uses: actions/[email protected]
- name: Set up JDK 8
uses: actions/setup-java@v1
with:
java-version: 8
- name: Set up Codecov
run: pip install --user codecov
- name: Build with Maven
run: mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
- name: Test with Maven
run: mvn test -B
- name: Codecov
uses: codecov/[email protected]
with:
name: Codecov on jdk 8
fail_ci_if_error: true
strategy:
fail-fast: false
matrix:
java: [ 8, 11, 16, 17 ]

build-on-jdk-11:
runs-on: ubuntu-latest
steps:
- name: Checkout scm
uses: actions/[email protected]
- name: Set up JDK 11
uses: actions/setup-java@v1
uses: actions/checkout@v4

- name: Set up jdk
uses: actions/setup-java@v3
with:
java-version: 11
- name: Set up Codecov
run: pip install --user codecov
distribution: 'zulu'
java-version: ${{ matrix.java }}

- name: Build with Maven
run: mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -B -V

- name: Test with Maven
run: mvn test -B

- name: Codecov
uses: codecov/codecov-action@v1.2.1
uses: codecov/codecov-action@v3
with:
name: Codecov on jdk 11
fail_ci_if_error: true
name: Codecov on jdk ${{ matrix.java }}
fail_ci_if_error: false
99 changes: 99 additions & 0 deletions src/main/java/com/bestvike/Index.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.bestvike;

import com.bestvike.linq.exception.ExceptionArgument;
import com.bestvike.linq.exception.ThrowHelper;

/**
* Represent a type can be used to index a collection either from the start or the end.
* <p>
* <pre>
* {@code
* List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
* Integer lastElement = Linq.of(list).elementAt(Index.fromEnd(1)); // lastElement = 5
* }
* </pre>
* <p>
* Created by 许崇雷 on 2023-05-31.
*/
public final class Index {
public static final Index Start = new Index(0);
public static final Index End = new Index(~0);
private final int value;

// The following private constructors mainly created for perf reason to avoid the checks
private Index(int value) {
this.value = value;
}

public Index(int value, boolean fromEnd) {
if (value < 0)
ThrowHelper.throwArgumentOutOfRangeException(ExceptionArgument.value);
if (fromEnd) {
this.value = ~value;
} else {
this.value = value;
}
}

public static Index fromStart(int value) {
if (value < 0)
ThrowHelper.throwArgumentOutOfRangeException(ExceptionArgument.value);
return new Index(value);
}

public static Index fromEnd(int value) {
if (value < 0)
ThrowHelper.throwArgumentOutOfRangeException(ExceptionArgument.value);
return new Index(~value);
}

public int getValue() {
if (this.value < 0)
return ~this.value;
else
return this.value;
}

public boolean isFromEnd() {
return this.value < 0;
}

public int getOffset(int length) {
int offset = this.value;
if (this.isFromEnd()) {
offset += length + 1;
}
return offset;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Index) {
return this.value == ((Index) obj).value;
}
return false;
}

@Override
public int hashCode() {
return this.value;
}

@Override
@SuppressWarnings("MethodDoesntCallSuperMethod")
protected Index clone() {
return new Index(this.value);
}

@Override
public String toString() {
if (this.isFromEnd()) {
return this.toStringFromEnd();
}
return String.valueOf(this.value);
}

private String toStringFromEnd() {
return '^' + String.valueOf(this.getValue());
}
}
106 changes: 106 additions & 0 deletions src/main/java/com/bestvike/Range.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.bestvike;

import com.bestvike.linq.exception.ExceptionArgument;
import com.bestvike.linq.exception.ThrowHelper;

/**
* Represent a range has start and end indexes.
* <p>
* <pre>
* {@code
* List<Integer> someArray = Arrays.asList(1, 2, 3, 4, 5);
* Integer[] subArray1 = Linq.of(someArray).take(Range.endAt(Index.fromStart(2))).toArray(Integer.class); // { 1, 2 }
* Integer[] subArray2 = Linq.of(someArray).take(Range.startAt(Index.fromStart(1))).toArray(Integer.class); // { 2, 3, 4, 5 }
* }
* </pre>
* <p>
* Created by 许崇雷 on 2023-05-31.
*/
public final class Range {
public static final Range All = new Range(Index.Start, Index.End);
private final Index start;
private final Index end;

public Range(Index start, Index end) {
if (start == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.start);
if (end == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.end);
this.start = start;
this.end = end;
}

public static Range startAt(Index start) {
return new Range(start, Index.End);
}

public static Range endAt(Index end) {
return new Range(Index.Start, end);
}

public Index getStart() {
return this.start;
}

public Index getEnd() {
return this.end;
}

public OffsetLength getOffsetAndLength(int length) {
int start;
Index startIndex = this.getStart();
if (startIndex.isFromEnd())
start = length - startIndex.getValue();
else
start = startIndex.getValue();

int end;
Index endIndex = this.getEnd();
if (endIndex.isFromEnd())
end = length - endIndex.getValue();
else
end = endIndex.getValue();

if (Integer.compareUnsigned(end, length) > 0 || Integer.compareUnsigned(start, end) > 0)
ThrowHelper.throwArgumentOutOfRangeException(ExceptionArgument.length);

return new OffsetLength(start, end - start);
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Range) {
Range r = (Range) obj;
return this.start.equals(r.start) && this.end.equals(r.end);
}
return false;
}

@Override
public int hashCode() {
int h1 = this.start.hashCode();
int h2 = this.end.hashCode();
return (h1 << 5) + h1 ^ h2;
}

@Override
@SuppressWarnings("MethodDoesntCallSuperMethod")
protected Range clone() {
return new Range(this.start, this.end);
}

@Override
public String toString() {
return this.start.toString() + ".." + this.end.toString();
}

public static final class OffsetLength {
public final int Offset;
public final int Length;

public OffsetLength(int offset, int length) {
this.Offset = offset;
this.Length = length;
}
}
}
Loading