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

Elimate Code Duplication in DefaultRedisList #2996

Closed
kjb512 opened this issue Sep 14, 2024 · 0 comments
Closed

Elimate Code Duplication in DefaultRedisList #2996

kjb512 opened this issue Sep 14, 2024 · 0 comments
Assignees
Labels
type: task A general task

Comments

@kjb512
Copy link

kjb512 commented Sep 14, 2024

There is code duplication in the DefaultRedisList methods. Specifically, the usage of listOps.rightPush/leftPush followed by a call to cap() is repeated in multiple places. This can be refactored to enhance code maintainability and readability.


	@Override
	public boolean add(E value) {
		listOps.rightPush(value);
		cap();
		return true;
	}
	@Override
	public boolean offer(E element) {
		listOps.rightPush(element);
		cap();
		return true;
	}
	@Override
	public void addFirst(E element) {
		listOps.leftPush(element);
		cap();
	}
	@Override
	public void add(int index, E element) {

		if (index == 0) {
			listOps.leftPush(element);
			cap();
			return;
		}

		int size = size();

		if (index == size()) {
			listOps.rightPush(element);
			cap();
			return;
		}

		if (index < 0 || index > size) {
			throw new IndexOutOfBoundsException();
		}

		throw new IllegalArgumentException("Redis supports insertion only at the beginning or the end of the list");
	}

Proposed Solution:

To enhance consistency and readability, I propose refactoring the method names to addFirst and addLast. This will provide a clear indication of where the element is being added, compared to the more generic add.

AS-IS

	@Override
	public void add(int index, E element) {

		if (index == 0) {
			listOps.leftPush(element);
			cap();
			return;
		}

		int size = size();

		if (index == size()) {
			listOps.rightPush(element);
			cap();
			return;
		}

		if (index < 0 || index > size) {
			throw new IndexOutOfBoundsException();
		}

		throw new IllegalArgumentException("Redis supports insertion only at the beginning or the end of the list");
	}

TO-BE

	@Override
	public void add(int index, E element) {

		if (index == 0) {
			addFirst(element);
			return;
		}

		int size = size();

		if (index == size()) {
			addLast(element);
			return;
		}

		if (index < 0 || index > size) {
			throw new IndexOutOfBoundsException();
		}

		throw new IllegalArgumentException("Redis supports insertion only at the beginning or the end of the list");
	}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 14, 2024
@mp911de mp911de self-assigned this Sep 16, 2024
@mp911de mp911de added type: task A general task and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 17, 2024
mp911de pushed a commit that referenced this issue Sep 17, 2024
mp911de added a commit that referenced this issue Sep 17, 2024
Add missing Nullable annotations. Reorder methods.

See: #2996
Original pull request: #2997
mp911de pushed a commit that referenced this issue Sep 17, 2024
mp911de added a commit that referenced this issue Sep 17, 2024
Add missing Nullable annotations. Reorder methods.

See: #2996
Original pull request: #2997
mp911de added a commit that referenced this issue Sep 17, 2024
Add missing Nullable annotations. Reorder methods.

See: #2996
Original pull request: #2997
@mp911de mp911de added this to the 3.2.11 (2023.1.11) milestone Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: task A general task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants