-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
186 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
cosid-core/src/main/java/me/ahoo/cosid/support/CosIdAnnotationSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright [2021-2021] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.support; | ||
|
||
import me.ahoo.cosid.annotation.CosId; | ||
import me.ahoo.cosid.provider.IdGeneratorProvider; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author ahoo wang | ||
*/ | ||
public class CosIdAnnotationSupport { | ||
|
||
private final IdGeneratorProvider idGeneratorProvider; | ||
private final ConcurrentHashMap<Class<?>, List<CosIdField>> classMapField; | ||
|
||
public CosIdAnnotationSupport(IdGeneratorProvider idGeneratorProvider) { | ||
this.classMapField = new ConcurrentHashMap<>(); | ||
this.idGeneratorProvider = idGeneratorProvider; | ||
} | ||
|
||
public void ensureId(Object entity) { | ||
getCosIdFields(entity.getClass()).forEach(cosIdField -> cosIdField.ensureId(entity, idGeneratorProvider)); | ||
} | ||
|
||
private List<CosIdField> getCosIdFields(Class<?> entityClass) { | ||
return classMapField.computeIfAbsent(entityClass, (key) -> Arrays.stream(entityClass.getDeclaredFields()) | ||
.filter(field -> field.isAnnotationPresent(CosId.class)) | ||
.map(field -> { | ||
CosId cosId = field.getAnnotation(CosId.class); | ||
return new CosIdField(cosId, field); | ||
}) | ||
.collect(Collectors.toList())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
cosid-core/src/test/java/me/ahoo/cosid/support/CosIdAnnotationSupportTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright [2021-2021] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.support; | ||
|
||
import me.ahoo.cosid.annotation.CosId; | ||
import me.ahoo.cosid.jvm.JdkId; | ||
import me.ahoo.cosid.provider.DefaultIdGeneratorProvider; | ||
import me.ahoo.cosid.provider.IdGeneratorProvider; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* @author ahoo wang | ||
*/ | ||
public class CosIdAnnotationSupportTest { | ||
private IdGeneratorProvider idGeneratorProvider = DefaultIdGeneratorProvider.INSTANCE; | ||
private CosIdAnnotationSupport cosIdSupport = new CosIdAnnotationSupport(idGeneratorProvider); | ||
|
||
@Test | ||
void ensureId() { | ||
idGeneratorProvider.setShare(JdkId.INSTANCE); | ||
Entity entity=new Entity(); | ||
cosIdSupport.ensureId(entity); | ||
Assertions.assertEquals(1,entity.getId()); | ||
Assertions.assertEquals("2",entity.getStringId()); | ||
} | ||
|
||
@Test | ||
void ensureIdNotFindIdGen() { | ||
MissingIdGenEntity entity=new MissingIdGenEntity(); | ||
Assertions.assertThrows(IllegalArgumentException.class,()->{ | ||
cosIdSupport.ensureId(entity); | ||
}); | ||
} | ||
|
||
|
||
public static class MissingIdGenEntity{ | ||
@CosId("missing") | ||
private long id; | ||
|
||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
cosid-core/src/test/java/me/ahoo/cosid/support/Entity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright [2021-2021] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.support; | ||
|
||
import me.ahoo.cosid.annotation.CosId; | ||
|
||
/** | ||
* @author ahoo wang | ||
*/ | ||
public class Entity { | ||
|
||
@CosId | ||
private long id; | ||
|
||
@CosId | ||
private String stringId; | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getStringId() { | ||
return stringId; | ||
} | ||
|
||
public void setStringId(String stringId) { | ||
this.stringId = stringId; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.