Skip to content

Instantly share code, notes, and snippets.

View vbmendes's full-sized avatar

Vinícius Mendes vbmendes

View GitHub Profile
@RestController
@RequestMapping("/data")
public class DataController {
@GetMapping(value = "/")
public ResponseEntity<tPage<T>> paginate(Pageable pageable) {
List<T> listFromSource = source.getList();
Page<T> page = PaginationUtil.paginateList(pageable, listFromSource);
return ResponseEntity.ok(page);
}
public final class PaginationUtil {
private PaginationUtil(){}
public static <T> Page<T> paginateList(final Pageable pageable, List<T> list) {
int first = Math.min(new Long(pageable.getOffset()).intValue(), list.size());;
int last = Math.min(first + pageable.getPageSize(), list.size());
return new PageImpl<>(list.subList(first, last), pageable, list.size());
}
}
return Stream.of(this.getStringA(), this.getStringB())
.filter(Objects::nonNull)
.collect(Collectors.joining(" - "));
if (this.getStringA() != null && this.getStringB() != null) {
return this.getStringA() + " - " + this.getStringB();
} else if (this.getStringA() != null) {
return this.getStringA();
} else if (this.getStringB() != null) {
return this.getStringB();
} else {
return "";
}
@vbmendes
vbmendes / NotNullSafe.java
Last active April 15, 2021 19:00
Joining strings in Java 8 with null safety
return this.getStringA() + " - " + this.getStringB();
@vbmendes
vbmendes / sublpymodule.py
Created April 3, 2013 17:42
Script to open source code of python modules in sublime. Useful to see your dependencies source code.
#!/usr/bin/env python
# coding: utf8
import imp
import sys, os
module_name = sys.argv[1]
bits = module_name.split('.')
name = imp.find_module(bits[0])[1]
if len(bits) > 1:
@vbmendes
vbmendes / test_hash.py
Created March 11, 2012 03:38 — forked from rafaelcaricio/.gitignore
test_hash.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
cars = json.loads(open('./fixtures/fipe_carro.json').read())
tree = {}
for v in cars:
for name in v['translated_names']:
@vbmendes
vbmendes / example.py
Created March 9, 2012 17:24
Exemplo de uso de yield e __iter__
def fib(quantidade):
a, b = 0, 1
for i in xrange(quantidade):
yield b
a, b = b, a + b
class Fib(object):
def __init__(self, quantidade):
@vbmendes
vbmendes / cached_property.py
Created February 13, 2012 04:07
cached_property decorator
from functools import wraps
def cached_property(fn):
@wraps(fn)
def wrapper(self):
cache_var = '_%s' % fn.__name__
if not hasattr(self, cache_var):
setattr(self, cache_var, fn(self))
return getattr(self, cache_var)
return property(wrapper)
@vbmendes
vbmendes / group_users_form.py
Created February 1, 2012 02:19 — forked from rafaelnovello/group_users_form.py
Select users to the group in group form on Django Admin.
# -*- coding: utf-8 -*-
from django.forms import ModelForm
from django import forms
from django.contrib.auth.models import Group, User
class GroupAdminForm(ModelForm):
class Meta:
model = Group
group_users = forms.ModelMultipleChoiceField(label=u'Usuários deste Grupo', queryset=User.objects.all())